Как устранить ошибку «Значение аргумента« dataOrField »не является допустимым документом Firestore» при использовании firestore.FieldValue.increment (1)? - PullRequest
1 голос
/ 03 августа 2020

Я выполняю простую операцию, пытаясь увеличить счетчик поля. код ниже:

import * as firebase from 'firebase';

const DocumentRef = db.doc(`pages/${request.params.pageId}`);

const increment = firebase.firestore.FieldValue.increment(1);

DocumentRef
    .get()
    .then(function (documentSnapshot) {
        if(!documentSnapshot.exists){
            return response.status(404).json({"message":"page not found"})
        }else{
            return db.collection(`comments`).add(newComment)
        }
    })
    .then(()=>{
        return DocumentRef.update({nComments: increment})
    })
    .then(() => {
        response.status(200).json(newComment);
    })
    .catch((error) => {
        console.log(error)
        return response.status(500).json({message:error.code});
    })

приводит к следующей ошибке:

Error: Update() requires either a single JavaScript object or an alternating list of field/value pairs that can be followed by an optional precondition. Value for argument "dataOrField" is not a valid Firestore document. Couldn't serialize object of type "FieldValueDelegate" (found in field "nComments"). Firestore doesn't support JavaScript objects with custom prototypes (i.e. objects that were created via the "new" operator).
    at WriteBatch.update (/srv/node_modules/@google-cloud/firestore/build/src/write-batch.js:374:23)
    at DocumentReference.update (/srv/node_modules/@google-cloud/firestore/build/src/reference.js:377:14)
    at screamDocumentRef.get.then.then (/srv/lib/handlers/screams.js:105:34)
    at <anonymous> 

Сообщение:

Update () требует либо одного JavaScript или альтернативный список пар поле / значение, за которым может следовать необязательное предварительное условие. Значение аргумента "dataOrField" не является допустимым документом Firestore. Не удалось сериализовать объект типа «FieldValueDelegate» (найден в поле «nComments»). Firestore не поддерживает объекты JavaScript с настраиваемыми прототипами (т. Е. Объекты, которые были созданы с помощью оператора «new»).

эта ошибка возникает только тогда, когда я добавляю эту цепочку «then»:

.then(()=>{
        return DocumentRef.update({nComments: increment})
    })

Не понимаю почему.

только соответствующая часть схемы, у меня 2 коллекции.

   collection: pages(nComments, body, etc)    id: pageId 
   collection: comments(pageId, etc)          id: commentId

when new comment document is added in comments collection, Im updating the count in pages collections document whose pageId is in comment documents pageId field.

nComments and pageId are fields.
...