【问题标题】:Error: Value for argument "data" is not a valid Firestore document. Cannot use "undefined" as a Firestore value (found in field "chatId")错误:参数“data”的值不是有效的 Firestore 文档。不能使用“未定义”作为 Firestore 值(在“chatId”字段中找到)
【发布时间】:2020-11-15 15:20:51
【问题描述】:

我一直在我的网站后端实现一个通知系统,当新消息创建时,数据库的结构如下图所示:

但是当创建消息时发生了这个错误:

错误

Error: Value for argument "data" is not a valid Firestore document. Cannot use "undefined" as a Firestore value (found in field "chatId"). If you want to ignore undefined values, enable `ignoreUndefinedProperties`.
    at validateUserInput (/workspace/node_modules/@google-cloud/firestore/build/src/serializer.js:271:19)
    at Object.validateUserInput (/workspace/node_modules/@google-cloud/firestore/build/src/serializer.js:263:13)
    at validateDocumentData (/workspace/node_modules/@google-cloud/firestore/build/src/write-batch.js:590:18)
    at WriteBatch.set (/workspace/node_modules/@google-cloud/firestore/build/src/write-batch.js:256:9)
    at DocumentReference.set (/workspace/node_modules/@google-cloud/firestore/build/src/reference.js:342:14)
    at /workspace/index.js:200:68
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

索引.js

exports.createNotificationOnFirstMessage=functions.region('europe-west1').firestore.document('/chats/{chatId}/messages/{messageId}')
.onCreate((snapshot)=>{
    var recipientArrn = snapshot.data().chatId.split('_');
    // recipientArrn is an array of substring, the first is the sender (index 0), the second is the recipient (index 1)
    return db.doc(`/users/${recipientArrn[1]}`).get()
            .then(doc =>{
                if(doc.exists){
                    return db.doc(`/notifications/${snapshot.id}`).set({
                        createdAt: new Date().toISOString(),
                        recipient: recipientArrn[1],
                        sender: snapshot.data().sender,
                        type:'message',
                        read: false,
                        chatId: doc.chatId,
                    });
                }
            })
            .catch(err =>{
                console.error(err);
            });
})

我可以做些什么来解决这个错误?

【问题讨论】:

    标签: node.js firebase google-cloud-platform google-cloud-firestore


    【解决方案1】:

    错误消息显示,您正在传递 undefined 的值:

    chatId: doc.chatId,
    

    所以 doc.chatIdundefined,这不是 Firestore 中字段的允许值。

    您可能正在寻找:

    chatId: doc.data().chatId,
    

    【讨论】:

    • 我已按照您的提示进行操作,但现在在此行出现错误:` return db.doc(/notifications/${snapshot.id}).set({ `
    • 这意味着原来的问题解决了,你现在有一个新的问题。我建议您尝试自己解决这个新问题,如果您无法解决,您可以发布一个新问题。
    猜你喜欢
    • 1970-01-01
    • 2020-01-16
    • 1970-01-01
    • 2020-02-26
    • 1970-01-01
    • 2018-09-26
    • 2018-10-11
    • 2018-04-15
    • 2020-06-13
    相关资源
    最近更新 更多