簡體   English   中英

Firebase部署后功能發生錯誤

[英]Error In function After Firebase Deploying

數據庫結構

在此處輸入圖片說明

通知結構

在此處輸入圖片說明

將功能成功部署到Fire-base后,我在Firebase功能日志中遇到這些錯誤

  1: Function execution took 1724 ms, finished with status: 'error'
  2:TypeError: Cannot read property 'uid' of undefined
   at exports.sendNotification.functions.firestore.document.onWrite.event 
   (/user_code/index.js:9:30)
   at cloudFunctionNewSignature (/user_code/node_modules/firebase- 
   functions/lib/cloud-functions.js:105:23)
   at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud- 
   functions.js:135:20)
   at /var/tmp/worker/worker.js:733:24
   at process._tickDomainCallback (internal/process/next_tick.js:135:7)
  3:{"@type":"type.googleapis.com/google.cloud.audit.AuditLog","status": 
    {"code":3,"message":"The request has errors"},"authenticationInfo": 
    {"principalEmail":"info.jap.123@gmail.com"},"requestMetadata"{"callerIp":"39.50.37.100",

Index.js

'use-strict'

 const functions = require('firebase-functions');
 const admin = require('firebase-admin');
 //admin.initializeApp(functions.config().firebase);
 exports.sendNotification = 
 functions.firestore. 
 document("Users/{uid}/Notification/{nid}").onWrite(event=> 
  {
   const user_id = event.params.uid
   const notification_id = event.params.nid;

 });

我在此類中的Notification Class將通知ID和消息存儲到Firebase-Firestore中。

  private void sendNotification()
{
    String number = edTxtDevId.getText().toString();
    if (TextUtils.isEmpty(number))
    {
        edTxtDevId.setError("Please Provide the dev id thank you");
        return;
    }
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
    String time = String.valueOf(cal.getTime());
    final String message = "Notification has been sent to this number "+number+" by this number "+user_id+" at time = "+time;
    Map<String , Object> notificationMessage = new HashMap<>();
    notificationMessage.put("From" , mUserId);
    notificationMessage.put("message" , message);
    for (int i = 0 ; i < userList.size() ; i++)
    {

        if (userList.get(i).equals(number))
        {
            Log.e(TAG, "sendNotification: "+number+" found " +userList.get(i) + " id = "+i);
            Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
            mFirestore.collection("Users/"+mUserId+"/Notification").add(notificationMessage).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
                @Override
                public void onSuccess(DocumentReference documentReference) {
                    Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
}

如果您知道如何解決這些問題,請幫助我,謝謝!

編輯:
首先,就像這里寫的一樣,您需要將帶有2個參數的函數傳遞給onWrite()
這是一個例子

就你而言
document("Users/{uid}/Notification/{nid}").onWrite(event=>應該是document("Users/{uid}/Notification/{nid}").onWrite((change, context) =>

因此,您的代碼應如下所示:

'use strict'

const functions = require('firebase-functions');
// const admin = require('firebase-admin');
// admin.initializeApp(functions.config().firebase);

exports.sendNotification = functions.firestore
  .document("Users/{uid}/Notification/{nid}")
  .onWrite((change, context) => { // <- changed arguments here
    // console.log(uid)
    // console.log(nid)
    // you can try these, but i think these console.log() will not work.
  });

其次,如果要從文檔路徑檢索uidnid ,則可以使用functions.database.DataSnapshot.reffunctions.EventContext.resource

因此您的代碼應如下所示:

'use strict'

const functions = require('firebase-functions');
// const admin = require('firebase-admin');
// admin.initializeApp(functions.config().firebase);

exports.sendNotification = functions.firestore
  .document("Users/{uid}/Notification/{nid}")
  .onWrite((change, context) => {
    // console.log(change.ref)
    // the output will be like: "Users/PfUyNf.../Notification/0Ujdt..."
    // or
    // console.log(context.resource)
    // the output will be like:
    // "projects/_/instances/<databaseInstance>/refs/Users/PfUyNf.../Notification/0Ujdt..."
  });

然后使用regexp或其他東西來處理輸出。

我認為會有更好的解決方案,但希望能有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM