Firebase模拟器始终返回错误:2在尝试了Firestore后台触发功能后未知?

u3r8eeie  于 2023-04-22  发布在  其他
关注(0)|答案(2)|浏览(110)

x1c 0d1x这是我的firestore(模拟器)看起来像
我正在尝试使用firebase模拟器来练习学习云函数,然而,我遇到这种情况的次数可能比我预期的要多,我希望这是我的问题。
我试着写一个函数,当用户发出https请求创建订单时,后台触发函数会将总量(数量 * 价格)返回给用户,后面的部分目前还是在制品;我目前只是试图了解和学习更多关于云功能。
这是https请求代码,我必须将项目,价格和数量添加到我的firestore。它工作得很好,并按照预期。

  1. exports.addCurrentOrder = functions.https.onRequest(async (req, res) => {
  2. const useruid = req.query.uid;
  3. const itemName = req.query.itemName;
  4. const itemPrice = req.query.itemPrice;
  5. const itemQuantity = req.query.itemQuantity;
  6. console.log('This is in useruid: ', useruid);
  7. const data = { [useruid] : {
  8. 'Item Name': itemName,
  9. 'Item Price': itemPrice,
  10. 'Item Quantity': itemQuantity,
  11. }};
  12. const writeResult = await admin.firestore().collection('Current Orders').add(data);
  13. res.json({result: data});
  14. });

这是给我各种错误的部分:

  1. exports.getTotal = functions.firestore.document('Current Orders/{documentId}').onCreate((snap, context) => {
  2. const data = snap.data();
  3. for(const i in data){
  4. console.log('This is in i: ', i);
  5. }
  6. return snap.ref.set({'testing': 'testing'}, {merge: true});
  7. });

每当我有这个,控制台总是会给予我:

  1. functions: Error: 2 UNKNOWN:
  2. at Object.callErrorFromStatus (/Users/user/firecast/functions/node_modules/@grpc/grpc-js/build/src/call.js:30:26)
  3. at Object.onReceiveStatus (/Users/user/firecast/functions/node_modules/@grpc/grpc-js/build/src/client.js:175:52)
  4. at Object.onReceiveStatus (/Users/user/firecast/functions/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:341:141)
  5. at Object.onReceiveStatus (/Users/user/firecast/functions/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:304:181)
  6. at Http2CallStream.outputStatus (/Users/user/firecast/functions/node_modules/@grpc/grpc-js/build/src/call-stream.js:116:74)
  7. at Http2CallStream.maybeOutputStatus (/Users/user/firecast/functions/node_modules/@grpc/grpc-js/build/src/call-stream.js:155:22)
  8. at Http2CallStream.endCall (/Users/user/firecast/functions/node_modules/@grpc/grpc-js/build/src/call-stream.js:141:18)
  9. at Http2CallStream.handleTrailers (/Users/user/firecast/functions/node_modules/@grpc/grpc-js/build/src/call-stream.js:273:14)
  10. at ClientHttp2Stream.<anonymous> (/Users/user/firecast/functions/node_modules/@grpc/grpc-js/build/src/call-stream.js:322:26)
  11. at ClientHttp2Stream.emit (events.js:210:5)
  12. Caused by: Error
  13. at WriteBatch.commit (/Users/user/firecast/functions/node_modules/@google-cloud/firestore/build/src/write-batch.js:415:23)
  14. at DocumentReference.create (/Users/user/firecast/functions/node_modules/@google-cloud/firestore/build/src/reference.js:283:14)
  15. at CollectionReference.add (/Users/user/firecast/functions/node_modules/@google-cloud/firestore/build/src/reference.js:2011:28)
  16. **at /Users/user/firecast/functions/index.js:43:76**
  17. at /usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:593:20
  18. at /usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:568:19
  19. at Generator.next (<anonymous>)
  20. at /usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:8:71
  21. at new Promise (<anonymous>)
  22. at __awaiter (/usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:4:12)
  23. Your function was killed because it raised an unhandled error.

即使我注解掉我认为给我错误的函数,我仍然会遇到这个问题(当我运行官方云函数指南上找到的示例函数时也是如此!)
我知道我在后台触发器上肯定做错了什么(希望有人能好心教我如何写这样的函数/让我开始)
我做错了什么?还是这是某种模拟器的错误?

vshtjzan

vshtjzan1#

我想我找到了。虽然我认为有更好的解决办法。
在全新的系统+firebase上,我使用了这个firebase模拟器教程来创建第一个名为makeUppercaseonCreate触发器,它工作了。然后我添加了你的getTotal,它不工作,也破坏了makeUppercase触发器!
我开始测试一些变化。尝试了很多次,最后,我已经从集合名称中删除了“空格”字符,如下所示:

  1. exports.getTotal = functions.firestore.document('CurrentOrders/{documentId}')...etc.

两个触发器也开始工作(使用Windows+ node 12的新VM)。它可能会在真实的Firestore示例上工作。因此,似乎集合名称中的“空格”在整个index.js中产生了一些错误。

kyvafyod

kyvafyod2#

如果没有为仿真函数定义LOCATION,也会发生此错误,您可以在终端中看到此错误,例如:

  1. functions[${LOCATION-fn]: firestore function initialized.

确保在环境变量中定义了合适的位置。

相关问题