【问题标题】:Platform exception while using google cloud function使用谷歌云功能时平台异常
【发布时间】:2020-10-15 12:56:54
【问题描述】:

我正在 flutter 中制作应用程序,并使用 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.

注意

  1. 我已将我的 Google Play 服务更新到最新版本。
  2. 我已获得清单文件中 INTERNET 的许可。
  3. 重新下载.json文件并检查

【问题讨论】:

    标签: javascript node.js firebase flutter google-cloud-functions


    【解决方案1】:

    您的 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 文档的特定字段的值,您可能应该使用 Transaction(这取决于您的具体用例)。

    【讨论】:

    • 我在答案的代码中添加了catch。请尝试分享您在 Cloud Functions 日志中遇到的错误。
    • 同平台异常
    • 问题中给出的
    • 您确认这是 Cloud Function 的错误(来自 Firebase 控制台),不是来自应用程序的错误(前端)
    • 是的,这完全是在添加云功能后发生的,firebase auth 和 firestore 等其他功能运行良好。
    猜你喜欢
    • 1970-01-01
    • 2018-05-27
    • 1970-01-01
    • 2020-06-02
    • 2020-07-26
    • 2020-05-09
    • 2020-08-06
    • 1970-01-01
    • 2019-06-07
    相关资源
    最近更新 更多