首页 > 解决方案 > 使用谷歌云功能时平台异常

问题描述

我正在制作一个应用程序并在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 文件并检查

标签: javascriptnode.jsfirebasefluttergoogle-cloud-functions

解决方案


您的 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(这取决于您的确切用例)。


推荐阅读