Firebase Firestore 云函数显示错误:无效使用类型“未定义”作为 Firestore 参数

新手上路,请多包涵

我有一个将货币详细信息添加到 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 许可协议

阅读 334
2 个回答

错误信息是这样的:

 Invalid use of type "undefined" as a Firestore argument.

您可以在堆栈跟踪中看到,当您使用 DocumentReference 上的对象调用 set() 时会发生这种情况。事实证明,您在对象中传递的值之一是未定义的。检查您传递的每个值并确保它们都具有实际值:

  .set({
   id : id,
   branchName : branchName,
   currencyName : currencyName,
   sellingRate : sellingRate,
   buyingRate :buyingRate
  });

不可能从错误消息中分辨出它是哪一个,所以你必须将它们全部打印出来做一些事情来检查它们中的每一个。

原文由 Doug Stevenson 发布,翻译遵循 CC BY-SA 3.0 许可协议

当您 .set 一个对象但对象中的一个字段未定义时,您将收到此错误。

问题是,当您使用 console.log 显示对象时,它不会显示未定义的变量,因此很难追踪。

使用以下内容而不是 console.log 来查找导致问题的元素。

 const util = require('util');
console.log(util.inspect(myObject, {showHidden: false, depth: null}));

这将为您提供如下输出:

 { origin: 'AMS',
  destination: undefined,
  duration: 94,
  carrier: 'KL',
  flight_number: '2977',
  departure: '2019-06-11T15:34:00',
  arrival: '2019-06-11T17:08:00',
  type: 'flight' }

原文由 Leonard Cremer 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题