【问题标题】:Error In function After Firebase DeployingFirebase 部署后功能出错
【发布时间】:2019-03-06 07:52:25
【问题描述】:

数据库结构

通知结构

成功将我的功能部署到 firebase 后,我在 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",

索引.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();
                }
            });
        }
    }
}

如果您知道如何解决这些问题,请帮助我 提前谢谢!

【问题讨论】:

  • 您是否使用在线控制台进行更改或从您的计算机进行部署?
  • 请添加数据库结构以查看您如何存储通知,请回复@。
  • @AlexMamo 请帮我解决这个问题,谢谢你提前
  • @hussainabbas 请添加数据库结构以查看您如何存储通知
  • @AlexMamo 这里是我的数据库用户的结构:{user_id:{Notification:{notification_id:{message,from}}}

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


【解决方案1】:

已编辑:
首先,正如写的here, 您需要将带有 2 个参数的函数传递给 onWrite()
Here is an example

在您的情况下,
document("Users/{uid}/Notification/{nid}").onWrite(event=&gt; 应该是 document("Users/{uid}/Notification/{nid}").onWrite((change, context) =&gt;

因此,您的代码应如下所示:

'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..."
  });

然后使用正则表达式或其他东西来处理输出。

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

【讨论】:

  • 那么,添加一些 console.log() 来查看您的参数。 context 是否已定义? context.params 是否已定义?如果已定义,它的成员中是否有 uid
  • 如果您是 firebase 新手,请注意云功能需要几分钟才能在部署完成后应用新代码。
  • 更改参数显示未定义,而上下文参数显示 [object object]
  • 嗯...我想让你检查context.params,而不是change。我认为context.params 不包含uid,所以发生了错误。
  • 是的,我想从文档路径中检索 uid 和 nid
猜你喜欢
  • 2018-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-19
  • 2021-09-25
  • 1970-01-01
  • 2021-12-01
相关资源
最近更新 更多