简体   繁体   English

使用 firestore.FieldValue.increment(1) 时如何解决错误“参数“dataOrField”的值不是有效的 Firestore 文档”?

[英]How to resolve error "Value for argument "dataOrField" is not a valid Firestore document" while using firestore.FieldValue.increment(1)?

I am performing a simple operation where in im trying to increment the count of a field.我正在执行一个简单的操作,我试图增加一个字段的计数。 the code is below:代码如下:

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});
    })

ending up in following error:最终出现以下错误:

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> 

The message is:消息是:

Update() requires either a single JavaScript object or an alternating list of field/value pairs that can be followed by an optional precondition. Update() 需要单个 JavaScript object 或可以后跟可选前提条件的交替字段/值对列表。 Value for argument "dataOrField" is not a valid Firestore document.参数“dataOrField”的值不是有效的 Firestore 文档。 Couldn't serialize object of type "FieldValueDelegate" (found in field "nComments").无法序列化“FieldValueDelegate”类型的 object(在字段“nComments”中找到)。 Firestore doesn't support JavaScript objects with custom prototypes (ie objects that were created via the "new" operator). Firestore 不支持具有自定义原型的 JavaScript 对象(即通过“new”运算符创建的对象)。

this error happens only when I add this "then" chain:仅当我添加此“then”链时才会发生此错误:

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

I do not understand why.我不懂为什么。

only relevant part of the schema, I've 2 collections.只有架构的相关部分,我有 2 collections。

   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.

You're probably using admin namespace to do the update while using increment from the firebase namespace.您可能正在使用admin命名空间进行更新,同时使用来自firebase命名空间的increment Try changing your increment statement to:尝试将增量语句更改为:

import * as admin from 'firebase-admin';
const increment = admin.firestore.FieldValue.increment(1);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 参数“数据”的值不是有效的 Firestore 文档 - Value for argument "data" is not a valid Firestore document Cloud Firestore 文档添加给出错误“参数“数据”的值不是有效的 Firestore 文档。不能使用“未定义”作为 Firestore 值” - Cloud Firestore document add gives error "Value for argument "data" is not a valid Firestore document. Cannot use "undefined" as a Firestore value" 错误:参数“数据”的值不是有效的 Firestore 文档。 不能将“undefined”用作 Firestore 值(在字段“chatId”中找到) - Error: Value for argument "data" is not a valid Firestore document. Cannot use "undefined" as a Firestore value (found in field "chatId") Firebase Firestore Increment FieldValue 不递增 - Firebase Firestore Increment FieldValue does not increment Firestore中的Cloud Functions FieldValue增量TypeError - Cloud Functions FieldValue increment TypeError in Firestore Swift:如何在 document().setData() Firestore 数据库中使用 FieldValue.serverTimestamp()? - Swift: How to use FieldValue.serverTimestamp() in document().setData() Firestore database? 如何将 Firestore 文档更新限制为仅在规则中增加一? - How to restrict Firestore document update to only increment by one in rules? 如何在 python 中增加 Firestore 字段值? - How to increment Firestore field value in python? Firestore 规则 - 更新具有 FieldValue.delete() 的文档 - Firestore Rules - Update document that has FieldValue.delete() 从 Firestore 文档中检索数字时出错 - Getting an error while retrieving a number from a Firestore document
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM