javascript - 使用谷歌云功能时平台异常
问题描述
我正在制作一个应用程序并在firebase中使用云功能来更新Firestone中的数字,
index.js
// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');
// The Firebase Admin SDK to access Cloud Firestore.
const admin = require('firebase-admin');
admin.initializeApp();
exports.getRandomNumbers=functions.https.onCall((context)=>
{
const numbers=admin.firestore().collection('RandomNumbers').document('CurrentRandomNumber');
return numbers.add({
RandomNumber=5;
});
});
这就是功能
onPressed: () async{
final HttpsCallable callable = CloudFunctions.instance.getHttpsCallable(
functionName: 'getRandomNumbers',
);
await callable.call().catchError((error){
print('$error');
});
},
即将出现的错误是 PlatformException(functionsError, Cloud function failed with exception., {code: NOT_FOUND, details: null, message: NOT_FOUND})
我还在我的日志中发现了这些,我可能认为这就是原因
W/DynamiteModule(12544): Local module descriptor class for providerinstaller not found.
I/DynamiteModule(12544): Considering local module providerinstaller:0 and remote module providerinstaller:0
W/ProviderInstaller(12544): Failed to load providerinstaller module: No acceptable module found. Local version is 0 and remote version is 0.
笔记
- 我已将我的 Google Play 服务更新到最新版本。
- 我已经在清单文件中获得了INTERNET的许可。
- 重新下载 .json 文件并检查
解决方案
您的 Callable Cloud Function 中的代码不正确。
通过做
const numbers = admin.firestore().collection('RandomNumbers').document('CurrentRandomNumber');
您正在声明一个DocumentReference
. DocumentReference 没有add()
方法。
如果要获取此文档的特定字段的值,则应使用异步get()
方法,如下所示:
exports.getRandomNumbers = functions.https.onCall((data, context) => {
const numbers = admin.firestore().collection('RandomNumbers').document('CurrentRandomNumber');
return numbers.get().then(documentSnapshot => {
if (documentSnapshot.exists) {
console.log('Document retrieved successfully.');
const aNumber = documentSnapshot.data().aNumber;
// Do something
// ...
return null; // or return the promise returned by an asynchronous method call,
// see https://firebase.google.com/docs/functions/terminate-functions?authuser=1
} else {
console.log('Document does not exist.');
return null;
}
})
});
如果您想增加 Firestore 文档的特定字段的值,您可能应该使用 a Transaction
(这取决于您的确切用例)。
推荐阅读
- c++ - 使用 for 循环在 C++ 中打印倒置的半空心金字塔时出错
- python-3.x - 如何使用多处理将 CSV 文件转换为 GZIP 文件。Python中的池
- android - 类型不匹配 - 必需:上下文,找到:片段 (Kotlin)
- flutter - Flutter Switch 动画 Bug
- c - 如何正确读取进程的文件描述符?
- javascript - 在 API 响应数据中格式化 ISO 日期
- keras - Zorro, R, keras 安装, rterm.exe
- c# - 如何保护 Excel 文件中的某些列不被修改?
- webpack - 是否可以将 wasm-bindgen 与 webpack 5 一起使用?
- python - 将代码上传到 AWS lambda 错误 Runtime.ImportModuleError