Firebase部署后功能发生错误

时间:2018-10-01 08:21:39

标签: javascript android firebase firebase-realtime-database google-cloud-firestore

数据库结构

enter image description here

通知结构

enter image description here

在将功能成功部署到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;

 });

该类中的通知类,我将通知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();
                }
            });
        }
    }
}

如果您知道如何解决这些问题,请帮助我 预先谢谢你!

1 个答案:

答案 0 :(得分:0)

已编辑:
首先,就像here那样, 您需要将具有2个参数的函数传递给onWrite()
Here is an example

就您而言,
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.ref functions.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或其他东西来处理输出。

我认为会有更好的解决方案,但希望能有所帮助。