有什么方法可以使用 Twilio Functions 读取和更新 Firestore 中的数据吗?

Is there any way to read and update data in Firestore using Twilio Functions?

我正在尝试使用以下代码从 Firestore 读取数据。我在依赖文件中添加了 firebase 依赖。但是似乎 运行 除了最后的模板代码之外什么都没有。我还将 firestore 的读取规则设置为 true 以进行检查。我什至不确定 Twilio 是否可以用于此。


var firebase = require('firebase');

exports.handler = function(context, event, callback) {
  
  var firebaseConfig = {
    apiKey: "[API_KEY]",
    authDomain: "[AUTH_DOMAIN]",
    projectId: "[PROJECT_ID]",
    storageBucket: "[STORAGE_BUCKET]",
    messagingSenderId: "[MESSAGING_SENDER_ID]",
    appId: "[APP_ID]"
  };
  
  if (!firebase.apps.length) {
    firebase.initializeApp(firebaseConfig);
    console.log('Initialized Firebase app');    
  }else {
    firebase.app();
    console.log('Firebase initialized');
  }
  
try{
  const userRef = firebase.db.collection('users').doc('123');
  const doc = userRef.get();
  if (!doc.exists) {
    console.log('No such document!');
  } else {
    console.log('Document data:', doc.data());
  }
} catch(e) {
  console.log(e);
}

  
  // Here's an example of setting up some TWiML to respond to with this function
    let twiml = new Twilio.twiml.VoiceResponse();
  twiml.say('Hello World');
    
  let variable = 'welcome!';

  // You can log with console.log
  console.log('error', variable);

  // This callback is what is returned in response to this function being invoked.
  // It's really important! E.g. you might respond with TWiML here for a voice or SMS response.
  // Or you might return JSON data to a studio flow. Don't forget it!
  return callback(null, twiml);
};

These are the dependencies added to the environment

此处为 Twilio 开发人员布道师。

我认为您的代码有两个问题,它们都与 JavaScript 的异步性质有关。

您没有将任何 Firebase 函数视为异步函数。事实证明,大多数是同步的,但是当您在 userRef 上调用 .get 时,那是一个异步调用。由于您正在使用 try/catch,我们可以通过将整个 Twilio 函数定义为 async 函数来快速解决此问题。

exports.handler = async function(context, event, callback) {

然后您可以在 userRef.get() 之前使用 await,事情应该开始起作用了。

  const doc = await userRef.get();

第二个问题是您进行了异步调用,并没有在最终调用 callback 之前等待响应。一旦调用 callback 函数,函数执行的其余部分将终止。因此,虽然实际上对 userRef.get() 进行了异步调用,但一旦函数到达 callback,它就被取消了,因为您没有等待结果。

在这两种情况下,添加 asyncawait 应该可以解决问题,您的函数应该 运行 符合预期。

这是添加了 asyncawait 的整个函数:

var firebase = require('firebase');

exports.handler = async function(context, event, callback) {
  
  var firebaseConfig = {
    apiKey: "[API_KEY]",
    authDomain: "[AUTH_DOMAIN]",
    projectId: "[PROJECT_ID]",
    storageBucket: "[STORAGE_BUCKET]",
    messagingSenderId: "[MESSAGING_SENDER_ID]",
    appId: "[APP_ID]"
  };
  
  if (!firebase.apps.length) {
    firebase.initializeApp(firebaseConfig);
    console.log('Initialized Firebase app');    
  }else {
    firebase.app();
    console.log('Firebase initialized');
  }
  
try{
  const userRef = firebase.db.collection('users').doc('123');
  const doc = await userRef.get();
  if (!doc.exists) {
    console.log('No such document!');
  } else {
    console.log('Document data:', doc.data());
  }
} catch(e) {
  console.log(e);
}

  
  // Here's an example of setting up some TWiML to respond to with this function
    let twiml = new Twilio.twiml.VoiceResponse();
  twiml.say('Hello World');
    
  let variable = 'welcome!';

  // You can log with console.log
  console.log('error', variable);

  // This callback is what is returned in response to this function being invoked.
  // It's really important! E.g. you might respond with TWiML here for a voice or SMS response.
  // Or you might return JSON data to a studio flow. Don't forget it!
  return callback(null, twiml);
};