我有一个将货币详细信息添加到 firestore 数据库的项目,我的项目正在使用 ionic 3
每当我向集合中添加新文档时,触发函数 onCreate() 将执行并更新名为“已更新”的文档。
但是触发功能总是显示错误。
Error: Invalid use of type "undefined" as a Firestore argument.
at Object.exports.customObjectError.val [as customObjectError] (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/validate.js:164:14)
at Function.encodeValue (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/document.js:808:20)
at Function.encodeFields (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/document.js:678:36)
at Function.fromObject (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/document.js:218:55)
at WriteBatch.set (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/write-batch.js:291:39)
at DocumentReference.set (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/reference.js:419:8)
at Object.<anonymous> (/user_code/lib/index.js:28:10)
at next (native)
at /user_code/lib/index.js:7:71
at __awaiter (/user_code/lib/index.js:3:12)
有人请帮助..
我花了很多时间在上面。
这是代码:
import * as functions from 'firebase-functions';
const admin = require('firebase-admin');
admin.initializeApp();
exports.createCurrency = functions.firestore
.document('Exchange/{ExchangeId}')
.onCreate( async (snap, context) => {
const id: string = snap.data().id;
const branchName: string = snap.data().branchName;
const currencyName: string = snap.data().currencyName;
const buyingRate : string = snap.data().buyingRate;
const sellingRate : string = snap.data().sellingRate;
const newUser= admin.
firestore()
.doc(`Exchange/updated`)
.set({
id : id,
branchName : branchName,
currencyName : currencyName,
sellingRate : sellingRate,
buyingRate :buyingRate
});
return newUser;
});
原文由 pepe 发布,翻译遵循 CC BY-SA 4.0 许可协议
错误信息是这样的:
您可以在堆栈跟踪中看到,当您使用 DocumentReference 上的对象调用 set() 时会发生这种情况。事实证明,您在对象中传递的值之一是未定义的。检查您传递的每个值并确保它们都具有实际值:
不可能从错误消息中分辨出它是哪一个,所以你必须将它们全部打印出来做一些事情来检查它们中的每一个。