简体   繁体   English

Firebase部署后功能发生错误

[英]Error In function After Firebase Deploying

Database Structure 数据库结构

在此处输入图片说明

Notification Structure 通知结构

在此处输入图片说明

After successfully deploying my function into fire-base I am having these error in firebase function logs 将功能成功部署到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 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 in this class i am storing the notification id and message into Firebase-Firestore. 我在此类中的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();
                }
            });
        }
    }
}

Please help me if you know how to solve these Thank you in advance! 如果您知道如何解决这些问题,请帮助我,谢谢!

Edited: 编辑:
First of all, just as written here , You need to pass a function with 2 arguments to onWrite() . 首先,就像这里写的一样,您需要将带有2个参数的函数传递给onWrite()
Here is an example . 这是一个例子

In your case, 就你而言
document("Users/{uid}/Notification/{nid}").onWrite(event=> should be document("Users/{uid}/Notification/{nid}").onWrite((change, context) => document("Users/{uid}/Notification/{nid}").onWrite(event=>应该是document("Users/{uid}/Notification/{nid}").onWrite((change, context) =>

So, your code should look like: 因此,您的代码应如下所示:

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

Second, if you want to retrieve uid and nid from the document path, you can use functions.database.DataSnapshot.ref or functions.EventContext.resource 其次,如果要从文档路径检索uidnid ,则可以使用functions.database.DataSnapshot.reffunctions.EventContext.resource

so your code should look like: 因此您的代码应如下所示:

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

then use regexp or something to process the output. 然后使用regexp或其他东西来处理输出。

I think there will be better solutions, but hope it helps. 我认为会有更好的解决方案,但希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 部署计划的函数侦听器的 firebase 函数错误 - firebase function error deploying a scheduled function listener 部署创建 agora 令牌的 firebase function 时出错 - Error while deploying firebase function which creates agora token 部署Firebase云function时这个错误是什么意思 - What does this error mean when deploying Firebase cloud function 在 Firebase 上部署后 API 不起作用 - API is not working after deploying on Firebase 部署Firebase时解析错误 - Parsing error while deploying firebase 将函数部署到 Firestore 时出错: - Error in deploying function to firestore: 在 Express JS 中,post() 方法在部署后在 firebase 云 function 中运行良好,但在本地机器上无法运行 - In Express JS, post() method is working perfectly in firebase cloud function after deploying but not working in local machine 错误解析错误:将功能部署到Firebase时,无效的正则表达式标志 - error Parsing error: Invalid regular expression flag while deploying function to firebase 在Firebase上部署网站后,脚本不会更改。 同样的错误仍然存​​在 - Script doesn't change after deploying site on Firebase. Same error persists 错误:部署谷歌云 function 时找不到模块“firebase/app” - Error: Cannot find module 'firebase/app' When deploying google cloud function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM