gpt4 book ai didi

javascript - 使用 firestore.FieldValue.increment(1) 时如何解决错误 "Value for argument "dataOrField“不是有效的 Firestore 文档”?

转载 作者:行者123 更新时间:2023-12-03 16:01:48 30 4
gpt4 key购买 nike

我正在执行一个简单的操作,我试图增加一个字段的计数。代码如下:

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() 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).


仅当我添加此“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.

最佳答案

您可能正在使用 admin使用 increment 时进行更新的命名空间来自 firebase命名空间。尝试将您的增量语句更改为:

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

关于javascript - 使用 firestore.FieldValue.increment(1) 时如何解决错误 "Value for argument "dataOrField“不是有效的 Firestore 文档”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63232586/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com