Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 部署时出现错误firebase函数[必须适当处理承诺]_Javascript_Firebase_Google Cloud Firestore_Google Cloud Functions - Fatal编程技术网

Javascript 部署时出现错误firebase函数[必须适当处理承诺]

Javascript 部署时出现错误firebase函数[必须适当处理承诺],javascript,firebase,google-cloud-firestore,google-cloud-functions,Javascript,Firebase,Google Cloud Firestore,Google Cloud Functions,上周我编写了一段代码,它在firebase服务器上部署时没有任何错误。但现在我不能在另一个帐户上再次部署它,以免我更改代码 我的一个朋友在关于firebase新更新的文章中告诉我,但我没有找到任何解决方案 它显示了这些错误 Promises must be handled appropriately 及 第一个错误指向我的第一行,第二个错误指向结束“catch”块: import * as functions from 'firebase-functions'; import * as adm

上周我编写了一段代码,它在firebase服务器上部署时没有任何错误。但现在我不能在另一个帐户上再次部署它,以免我更改代码

我的一个朋友在关于firebase新更新的文章中告诉我,但我没有找到任何解决方案

它显示了这些错误

Promises must be handled appropriately

第一个错误指向我的第一行,第二个错误指向结束“catch”块:

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp();

// export const helloWorld = functions.https.onRequest((request, response) => {
//  console.log("sadegh");
//  response.send("Hello from Firebase1!");
// });
//
export const sendChatNotification = functions
.firestore.document('rooms/{roomId}/messages/{messageId}')
.onCreate((snap, context) => {



    const roomId = context.params.roomId;
    const messageId = context.params.messageId;

    const newValue = snap.data();

    const receiverId = newValue.receiverId;
    const text = newValue.text;
    const type = newValue.type;
    const senderName = newValue.senderName;


    var p = admin.firestore().collection("users").doc(receiverId).get();
    p.then(snapshot2 => {
        const data2 = snapshot2.data();
        const firebaseNotificationToken = data2.firebaseNotificationToken;
        // const name = data2.name;

        if (type == 'voiceCall' || type == 'videoCall' || type == 'hangUp') {


            const channelId = newValue.channelId;
            const senderId = newValue.senderId;
            const status = newValue.status;

            console.log("type: " + type + " /status: " + status)

            let message = {
                data: {
                    type: type,
                    senderId: senderId,
                    senderName: senderName,
                    receiverId: receiverId,
                    status: status,
                    channelId: channelId,
                    roomId: roomId
                },
                token: firebaseNotificationToken
            };

            sendMessage(message)


            if (status == "canceled") {
                let message1 = {
                    notification: {
                        title: '☎ Missed voice call ',
                        body: senderName
                    },
                    token: firebaseNotificationToken
                };
                sendMessage(message1)
            } else if ((type == 'voiceCall' || type == 'videoCall') && status = '') {
                let message1 = {
                    notification: {
                        title: '☎ ' + senderName + ' is calling you',
                        body: 'tap to answer...'
                    },
                    token: firebaseNotificationToken
                };
                sendMessage(message1)
            }


        } else {
            let message = {
                notification: {
                    title: 'The problem is you commented the return in your catch block
As your Firebase .get() function must return a promise, in your code, if it fails, it won't return a promise and it will hang there.

either use
return null
or return something to be handled by the calling app

Your code is a bit messy and it is not really easy to understand it without dedicating a long time.

However, here is below a piece of code that should work and that cover one case of your Business Logic. Note how the promises returned by the asynchronous tasks are returned.

  export const sendChatNotification = functions.firestore
      .document('rooms/{roomId}/messages/{messageId}')
      .onCreate((snap, context) => {
        const roomId = context.params.roomId;
        const messageId = context.params.messageId;

        const newValue = snap.data();

        const receiverId = newValue.receiverId;
        const text = newValue.text;
        const type = newValue.type;
        const senderName = newValue.senderName;

        var p = admin
          .firestore()
          .collection('users')
          .doc(receiverId)
          .get();

        return p.then(snapshot2 => {  // <- HERE, the promise is returned
          const data2 = snapshot2.data();
          const firebaseNotificationToken = data2.firebaseNotificationToken;

          if (type == 'voiceCall' || type == 'videoCall' || type == 'hangUp') {
            const channelId = newValue.channelId;
            const senderId = newValue.senderId;
            const status = newValue.status;

            console.log('type: ' + type + ' /status: ' + status);

            let message = {
              data: {
                type: type,
                senderId: senderId,
                senderName: senderName,
                receiverId: receiverId,
                status: status,
                channelId: channelId,
                roomId: roomId
              },
              token: firebaseNotificationToken
            };

            return admin.messaging().send(message);  // <- HERE, the promise is returned
          }
        });

  }); 
import*作为“firebase函数”中的函数;
从“firebase admin”导入*作为管理员;
admin.initializeApp();
//export const helloWorld=functions.https.onRequest((请求,响应)=>{
//console.log(“sadegh”);
//回复。发送(“来自Firebase1的你好!”);
// });
//
export const sendChatNotification=函数
.firestore.document('rooms/{roomId}/messages/{messageId}')
.onCreate((快照,上下文)=>{
const roomId=context.params.roomId;
const messageId=context.params.messageId;
const newValue=snap.data();
const receiverId=newValue.receiverId;
const text=newValue.text;
const type=newValue.type;
const senderName=newValue.senderName;
var p=admin.firestore().collection(“users”).doc(receiverId.get();
p、 然后(快照2=>{
const data2=snapshot2.data();
const firebaseNotificationToken=data2.firebaseNotificationToken;
//const name=data2.name;
如果(类型=='voiceCall'| |类型=='videoCall'| |类型=='hangUp'){
const channelId=newValue.channelId;
const senderId=newValue.senderId;
const status=newValue.status;
console.log(“类型:“+type+”/状态:“+status”)
让消息={
数据:{
类型:类型,
senderId:senderId,
senderName:senderName,
收款人:收款人,
状态:状态,
channelId:channelId,
室友
},
令牌:firebaseNotificationToken
};
发送消息(消息)
如果(状态=“已取消”){
让message1={
通知:{
标题:'☎ 未接语音呼叫',
正文:senderName
},
令牌:firebaseNotificationToken
};
发送消息(消息1)
}else if((类型==“voiceCall”| |类型==“videoCall”)&&status=“”){
让message1={
通知:{
标题:'☎ ' + senderName+'正在呼叫您',
身体:'点击回答…'
},
令牌:firebaseNotificationToken
};
发送消息(消息1)
}
}否则{
让消息={
通知:{

标题:“问题是您在catch块中对返回进行了注释 由于您的Firebase.get()函数必须返回一个承诺,在您的代码中,如果失败,它将不会返回承诺,并且将挂起


使用
return null
或返回调用应用程序要处理的内容

问题是您在catch块中对返回进行了注释 由于您的Firebase.get()函数必须返回一个承诺,在您的代码中,如果失败,它将不会返回承诺,并且将挂起


使用
return null
或返回要由调用应用程序处理的内容

您的代码有点凌乱,不花很长时间就很难理解

但是,下面是一段应该可以工作的代码,它涵盖了业务逻辑的一种情况。请注意异步任务返回的承诺是如何返回的

export const sendChatNotification=functions.firestore
.document('rooms/{roomId}/messages/{messageId}')
.onCreate((快照,上下文)=>{
const roomId=context.params.roomId;
const messageId=context.params.messageId;
const newValue=snap.data();
const receiverId=newValue.receiverId;
const text=newValue.text;
const type=newValue.type;
const senderName=newValue.senderName;
var p=admin
.firestore()
.collection('用户')
.doc(接收方)
.get();

返回p.then(snapshot2=>{/您的代码有点凌乱,不花很长时间就很难理解它

但是,下面是一段应该可以工作的代码,它涵盖了业务逻辑的一种情况。请注意异步任务返回的承诺是如何返回的

export const sendChatNotification=functions.firestore
.document('rooms/{roomId}/messages/{messageId}')
.onCreate((快照,上下文)=>{
const roomId=context.params.roomId;
const messageId=context.params.messageId;
const newValue=snap.data();
const receiverId=newValue.receiverId;
const text=newValue.text;
const type=newValue.type;
const senderName=newValue.senderName;
var p=admin
.firestore()
.collection('用户')
.doc(接收方)
.get();

return p.then(snapshot2=>{//您能在问题中包含云函数的完整代码吗?您的问题很可能来自这样一个事实,即您没有返回由
admin.firestore().collection(“users”).doc(receiverId).get()
返回的承诺,但我们需要完整的(即整个)代码以确认此假设。@RenaudTarnec添加了完整代码。感谢您的审阅感谢添加。坦率地说,您的代码有点凌乱,不花很长时间就很难理解……不过,下面是我的答案
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp();

// export const helloWorld = functions.https.onRequest((request, response) => {
//  console.log("sadegh");
//  response.send("Hello from Firebase1!");
// });
//
export const sendChatNotification = functions
.firestore.document('rooms/{roomId}/messages/{messageId}')
.onCreate((snap, context) => {



    const roomId = context.params.roomId;
    const messageId = context.params.messageId;

    const newValue = snap.data();

    const receiverId = newValue.receiverId;
    const text = newValue.text;
    const type = newValue.type;
    const senderName = newValue.senderName;


    var p = admin.firestore().collection("users").doc(receiverId).get();
    p.then(snapshot2 => {
        const data2 = snapshot2.data();
        const firebaseNotificationToken = data2.firebaseNotificationToken;
        // const name = data2.name;

        if (type == 'voiceCall' || type == 'videoCall' || type == 'hangUp') {


            const channelId = newValue.channelId;
            const senderId = newValue.senderId;
            const status = newValue.status;

            console.log("type: " + type + " /status: " + status)

            let message = {
                data: {
                    type: type,
                    senderId: senderId,
                    senderName: senderName,
                    receiverId: receiverId,
                    status: status,
                    channelId: channelId,
                    roomId: roomId
                },
                token: firebaseNotificationToken
            };

            sendMessage(message)


            if (status == "canceled") {
                let message1 = {
                    notification: {
                        title: '☎ Missed voice call ',
                        body: senderName
                    },
                    token: firebaseNotificationToken
                };
                sendMessage(message1)
            } else if ((type == 'voiceCall' || type == 'videoCall') && status = '') {
                let message1 = {
                    notification: {
                        title: '☎ ' + senderName + ' is calling you',
                        body: 'tap to answer...'
                    },
                    token: firebaseNotificationToken
                };
                sendMessage(message1)
            }


        } else {
            let message = {
                notification: {
                    title: 'The problem is you commented the return in your catch block
As your Firebase .get() function must return a promise, in your code, if it fails, it won't return a promise and it will hang there.

either use
return null
or return something to be handled by the calling app

Your code is a bit messy and it is not really easy to understand it without dedicating a long time.

However, here is below a piece of code that should work and that cover one case of your Business Logic. Note how the promises returned by the asynchronous tasks are returned.

  export const sendChatNotification = functions.firestore
      .document('rooms/{roomId}/messages/{messageId}')
      .onCreate((snap, context) => {
        const roomId = context.params.roomId;
        const messageId = context.params.messageId;

        const newValue = snap.data();

        const receiverId = newValue.receiverId;
        const text = newValue.text;
        const type = newValue.type;
        const senderName = newValue.senderName;

        var p = admin
          .firestore()
          .collection('users')
          .doc(receiverId)
          .get();

        return p.then(snapshot2 => {  // <- HERE, the promise is returned
          const data2 = snapshot2.data();
          const firebaseNotificationToken = data2.firebaseNotificationToken;

          if (type == 'voiceCall' || type == 'videoCall' || type == 'hangUp') {
            const channelId = newValue.channelId;
            const senderId = newValue.senderId;
            const status = newValue.status;

            console.log('type: ' + type + ' /status: ' + status);

            let message = {
              data: {
                type: type,
                senderId: senderId,
                senderName: senderName,
                receiverId: receiverId,
                status: status,
                channelId: channelId,
                roomId: roomId
              },
              token: firebaseNotificationToken
            };

            return admin.messaging().send(message);  // <- HERE, the promise is returned
          }
        });

  });