Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/367.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云函数';ForEach&x27;快照的子对象_Javascript_Firebase_Firebase Realtime Database_Twilio_Google Cloud Functions - Fatal编程技术网

Javascript 调用Firebase云函数';ForEach&x27;快照的子对象

Javascript 调用Firebase云函数';ForEach&x27;快照的子对象,javascript,firebase,firebase-realtime-database,twilio,google-cloud-functions,Javascript,Firebase,Firebase Realtime Database,Twilio,Google Cloud Functions,我正在尝试部署一个Firebase云函数,该函数向其关联的收件人发送x条文本消息。当对“发送”实时数据库引用进行更新时,我的iOS应用程序中会触发该功能,表明用户已按下“发送”按钮 我的火基结构是 { "user1uid": { "send": false "messagesToSend": { "messageuid1": { "messageText": "What's for dinner?", "recipientN

我正在尝试部署一个Firebase云函数,该函数向其关联的收件人发送x条文本消息。当对“发送”实时数据库引用进行更新时,我的iOS应用程序中会触发该功能,表明用户已按下“发送”按钮

我的火基结构是

  {
  "user1uid": {
    "send": false
    "messagesToSend": {
      "messageuid1": {
         "messageText": "What's for dinner?",
         "recipientNumber": "+18017378888",
      }
      "messageuid2:
         "messageText": "Who won the Cowboys game?",
         "recipientNumber": "+18017377787",
      }
   }
   "user2uid": {
    "send": false
    "messagesToSend": {
      "messageuid1": {
         "messageText": "What's for dinner?",
         "recipientNumber": "+18017378888",
      }
      "messageuid2:
         "messageText": "Who won the Cowboys game?",
         "recipientNumber": "+18017377787",
      }
   }
}
我的代码目前只发送一条消息,我不确定如何正确地迭代每个用户的messagesToSend节点并发送其中的所有消息

我一直在努力学习教程。我查看了以下堆栈溢出响应,但无法从中破译或导出解决方案:


我发送一条消息的index.js代码如下:

const functions = require('firebase-functions');

// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp();

const twilio = require('twilio')

const accountSid = functions.config().twilio.sid;
const authToken  = functions.config().twilio.token;

const client = new twilio(accountSid, authToken);

const twilioNumber = functions.config().twilio.number;


// Start cloud function
exports.sendSecrets = functions.database
       .ref('/{uid}/send')
       .onUpdate((change,context) => {

    const uid = context.params.uid;

    return admin.database().ref(uid+'/messagesToSend').once('value').then(snapshot => {

      snapshot.forEach(function(childSnapshot) {

          var key = childSnapshot.key;
          var messageData = childSnapshot.val();
          **if (messageData.sanitized) return true;**
          var message = messageData.messageText;
          var phoneNumber = messageData.recipientNumber;

          const textMessage = {
            body: `From My App - ${message}`,
            from: twilioNumber, // From Twilio number
            to: phoneNumber  // Text to this number
          }
          return client.messages.create(textMessage)
        })
        **return snapshot.ref.toString();**
    });
}); 
const functions = require('firebase-functions');

// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp();

const twilio = require('twilio')

const accountSid = functions.config().twilio.sid;
const authToken  = functions.config().twilio.token;

const client = new twilio(accountSid, authToken);

const twilioNumber = functions.config().twilio.number;


// Start cloud function
exports.sendSecrets = functions.database
       .ref('/{uid}/send')
       .onUpdate((change,context) => {

    const uid = context.params.uid;

    return admin.database().ref(uid+'/messagesToSend').once('value')
    .then(snapshot => {

      const promises = [];

      snapshot.forEach(function(childSnapshot) {

          var key = childSnapshot.key;
          var messageData = childSnapshot.val();
          //**if (messageData.sanitized) return true;**
          var message = messageData.messageText;
          var phoneNumber = messageData.recipientNumber;

          const textMessage = {
            body: `From My App - ${message}`,
            from: twilioNumber, // From Twilio number
            to: phoneNumber  // Text to this number
          }

          promises.push(client.messages.create(textMessage));
      })

      return Promise.all(promises);
    })
    // Edits made below to parentheses/brackets
    .then(results => {
      //Do whatever you want !!
      // e.g. print the results which will be an array of messages
      // (see https://www.twilio.com/docs/libraries/node#testing-your-installation)
    })            
}); 
....
     return Promise.all(promises);
   })
}); 

请注意,两端标有**的行表示我知道我需要根据收到的错误消息返回某些内容,这些错误消息指示“Each then()应返回值或抛出”。

我假设您使用的是使用承诺的
twilio节点
库:

由于要并行发送多条消息,因此必须使用
Promise.all()
,如下所示:

const functions = require('firebase-functions');

// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp();

const twilio = require('twilio')

const accountSid = functions.config().twilio.sid;
const authToken  = functions.config().twilio.token;

const client = new twilio(accountSid, authToken);

const twilioNumber = functions.config().twilio.number;


// Start cloud function
exports.sendSecrets = functions.database
       .ref('/{uid}/send')
       .onUpdate((change,context) => {

    const uid = context.params.uid;

    return admin.database().ref(uid+'/messagesToSend').once('value').then(snapshot => {

      snapshot.forEach(function(childSnapshot) {

          var key = childSnapshot.key;
          var messageData = childSnapshot.val();
          **if (messageData.sanitized) return true;**
          var message = messageData.messageText;
          var phoneNumber = messageData.recipientNumber;

          const textMessage = {
            body: `From My App - ${message}`,
            from: twilioNumber, // From Twilio number
            to: phoneNumber  // Text to this number
          }
          return client.messages.create(textMessage)
        })
        **return snapshot.ref.toString();**
    });
}); 
const functions = require('firebase-functions');

// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp();

const twilio = require('twilio')

const accountSid = functions.config().twilio.sid;
const authToken  = functions.config().twilio.token;

const client = new twilio(accountSid, authToken);

const twilioNumber = functions.config().twilio.number;


// Start cloud function
exports.sendSecrets = functions.database
       .ref('/{uid}/send')
       .onUpdate((change,context) => {

    const uid = context.params.uid;

    return admin.database().ref(uid+'/messagesToSend').once('value')
    .then(snapshot => {

      const promises = [];

      snapshot.forEach(function(childSnapshot) {

          var key = childSnapshot.key;
          var messageData = childSnapshot.val();
          //**if (messageData.sanitized) return true;**
          var message = messageData.messageText;
          var phoneNumber = messageData.recipientNumber;

          const textMessage = {
            body: `From My App - ${message}`,
            from: twilioNumber, // From Twilio number
            to: phoneNumber  // Text to this number
          }

          promises.push(client.messages.create(textMessage));
      })

      return Promise.all(promises);
    })
    // Edits made below to parentheses/brackets
    .then(results => {
      //Do whatever you want !!
      // e.g. print the results which will be an array of messages
      // (see https://www.twilio.com/docs/libraries/node#testing-your-installation)
    })            
}); 
....
     return Promise.all(promises);
   })
}); 
您也可以简单地返回如下所示的
Promise.all()

const functions = require('firebase-functions');

// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp();

const twilio = require('twilio')

const accountSid = functions.config().twilio.sid;
const authToken  = functions.config().twilio.token;

const client = new twilio(accountSid, authToken);

const twilioNumber = functions.config().twilio.number;


// Start cloud function
exports.sendSecrets = functions.database
       .ref('/{uid}/send')
       .onUpdate((change,context) => {

    const uid = context.params.uid;

    return admin.database().ref(uid+'/messagesToSend').once('value').then(snapshot => {

      snapshot.forEach(function(childSnapshot) {

          var key = childSnapshot.key;
          var messageData = childSnapshot.val();
          **if (messageData.sanitized) return true;**
          var message = messageData.messageText;
          var phoneNumber = messageData.recipientNumber;

          const textMessage = {
            body: `From My App - ${message}`,
            from: twilioNumber, // From Twilio number
            to: phoneNumber  // Text to this number
          }
          return client.messages.create(textMessage)
        })
        **return snapshot.ref.toString();**
    });
}); 
const functions = require('firebase-functions');

// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp();

const twilio = require('twilio')

const accountSid = functions.config().twilio.sid;
const authToken  = functions.config().twilio.token;

const client = new twilio(accountSid, authToken);

const twilioNumber = functions.config().twilio.number;


// Start cloud function
exports.sendSecrets = functions.database
       .ref('/{uid}/send')
       .onUpdate((change,context) => {

    const uid = context.params.uid;

    return admin.database().ref(uid+'/messagesToSend').once('value')
    .then(snapshot => {

      const promises = [];

      snapshot.forEach(function(childSnapshot) {

          var key = childSnapshot.key;
          var messageData = childSnapshot.val();
          //**if (messageData.sanitized) return true;**
          var message = messageData.messageText;
          var phoneNumber = messageData.recipientNumber;

          const textMessage = {
            body: `From My App - ${message}`,
            from: twilioNumber, // From Twilio number
            to: phoneNumber  // Text to this number
          }

          promises.push(client.messages.create(textMessage));
      })

      return Promise.all(promises);
    })
    // Edits made below to parentheses/brackets
    .then(results => {
      //Do whatever you want !!
      // e.g. print the results which will be an array of messages
      // (see https://www.twilio.com/docs/libraries/node#testing-your-installation)
    })            
}); 
....
     return Promise.all(promises);
   })
}); 

我假设您正在使用使用承诺的
twilio节点
库:

由于要并行发送多条消息,因此必须使用
Promise.all()
,如下所示:

const functions = require('firebase-functions');

// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp();

const twilio = require('twilio')

const accountSid = functions.config().twilio.sid;
const authToken  = functions.config().twilio.token;

const client = new twilio(accountSid, authToken);

const twilioNumber = functions.config().twilio.number;


// Start cloud function
exports.sendSecrets = functions.database
       .ref('/{uid}/send')
       .onUpdate((change,context) => {

    const uid = context.params.uid;

    return admin.database().ref(uid+'/messagesToSend').once('value').then(snapshot => {

      snapshot.forEach(function(childSnapshot) {

          var key = childSnapshot.key;
          var messageData = childSnapshot.val();
          **if (messageData.sanitized) return true;**
          var message = messageData.messageText;
          var phoneNumber = messageData.recipientNumber;

          const textMessage = {
            body: `From My App - ${message}`,
            from: twilioNumber, // From Twilio number
            to: phoneNumber  // Text to this number
          }
          return client.messages.create(textMessage)
        })
        **return snapshot.ref.toString();**
    });
}); 
const functions = require('firebase-functions');

// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp();

const twilio = require('twilio')

const accountSid = functions.config().twilio.sid;
const authToken  = functions.config().twilio.token;

const client = new twilio(accountSid, authToken);

const twilioNumber = functions.config().twilio.number;


// Start cloud function
exports.sendSecrets = functions.database
       .ref('/{uid}/send')
       .onUpdate((change,context) => {

    const uid = context.params.uid;

    return admin.database().ref(uid+'/messagesToSend').once('value')
    .then(snapshot => {

      const promises = [];

      snapshot.forEach(function(childSnapshot) {

          var key = childSnapshot.key;
          var messageData = childSnapshot.val();
          //**if (messageData.sanitized) return true;**
          var message = messageData.messageText;
          var phoneNumber = messageData.recipientNumber;

          const textMessage = {
            body: `From My App - ${message}`,
            from: twilioNumber, // From Twilio number
            to: phoneNumber  // Text to this number
          }

          promises.push(client.messages.create(textMessage));
      })

      return Promise.all(promises);
    })
    // Edits made below to parentheses/brackets
    .then(results => {
      //Do whatever you want !!
      // e.g. print the results which will be an array of messages
      // (see https://www.twilio.com/docs/libraries/node#testing-your-installation)
    })            
}); 
....
     return Promise.all(promises);
   })
}); 
您也可以简单地返回如下所示的
Promise.all()

const functions = require('firebase-functions');

// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp();

const twilio = require('twilio')

const accountSid = functions.config().twilio.sid;
const authToken  = functions.config().twilio.token;

const client = new twilio(accountSid, authToken);

const twilioNumber = functions.config().twilio.number;


// Start cloud function
exports.sendSecrets = functions.database
       .ref('/{uid}/send')
       .onUpdate((change,context) => {

    const uid = context.params.uid;

    return admin.database().ref(uid+'/messagesToSend').once('value').then(snapshot => {

      snapshot.forEach(function(childSnapshot) {

          var key = childSnapshot.key;
          var messageData = childSnapshot.val();
          **if (messageData.sanitized) return true;**
          var message = messageData.messageText;
          var phoneNumber = messageData.recipientNumber;

          const textMessage = {
            body: `From My App - ${message}`,
            from: twilioNumber, // From Twilio number
            to: phoneNumber  // Text to this number
          }
          return client.messages.create(textMessage)
        })
        **return snapshot.ref.toString();**
    });
}); 
const functions = require('firebase-functions');

// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp();

const twilio = require('twilio')

const accountSid = functions.config().twilio.sid;
const authToken  = functions.config().twilio.token;

const client = new twilio(accountSid, authToken);

const twilioNumber = functions.config().twilio.number;


// Start cloud function
exports.sendSecrets = functions.database
       .ref('/{uid}/send')
       .onUpdate((change,context) => {

    const uid = context.params.uid;

    return admin.database().ref(uid+'/messagesToSend').once('value')
    .then(snapshot => {

      const promises = [];

      snapshot.forEach(function(childSnapshot) {

          var key = childSnapshot.key;
          var messageData = childSnapshot.val();
          //**if (messageData.sanitized) return true;**
          var message = messageData.messageText;
          var phoneNumber = messageData.recipientNumber;

          const textMessage = {
            body: `From My App - ${message}`,
            from: twilioNumber, // From Twilio number
            to: phoneNumber  // Text to this number
          }

          promises.push(client.messages.create(textMessage));
      })

      return Promise.all(promises);
    })
    // Edits made below to parentheses/brackets
    .then(results => {
      //Do whatever you want !!
      // e.g. print the results which will be an array of messages
      // (see https://www.twilio.com/docs/libraries/node#testing-your-installation)
    })            
}); 
....
     return Promise.all(promises);
   })
}); 

谢谢你,勒诺,我刚刚编辑了一些括号/括号,毫无疑问,你打得很快。谢谢你的帮助。@northsydneybears很高兴我能帮忙。如果你认为这有帮助的话,你可以考虑我的回答。谢谢考虑过了,做过了,再次感谢你。你知道我会打什么电话把所有发送的信息数据打印出来吗?(例如,to、from和messageText/body?)我知道它是console.log,但我尝试了
。然后((message)=>console.log(message))并且它只返回收件人的号码。@northsydneybears抱歉,我不太了解Twilio SDK和API。但是根据doc(),
create()
方法返回一个用
消息
对象解析的承诺。这类对象的API文档如下:。除其他属性外,还有一些
from
to
属性。
Promise.all()
返回的消息将包含在
results
数组中。此数组的顺序与
数组的顺序相同。希望这有帮助!谢谢你,勒诺,我刚刚编辑了一些括号/括号,毫无疑问,你打得很快。谢谢你的帮助。@northsydneybears很高兴我能帮忙。如果你认为这有帮助的话,你可以考虑我的回答。谢谢考虑过了,做过了,再次感谢你。你知道我会打什么电话把所有发送的信息数据打印出来吗?(例如,to、from和messageText/body?)我知道它是console.log,但我尝试了
。然后((message)=>console.log(message))并且它只返回收件人的号码。@northsydneybears抱歉,我不太了解Twilio SDK和API。但是根据doc(),
create()
方法返回一个用
消息
对象解析的承诺。这类对象的API文档如下:。除其他属性外,还有一些
from
to
属性。
Promise.all()
返回的消息将包含在
results
数组中。此数组的顺序与
数组的顺序相同。希望这有帮助!