【问题标题】:Is there any way to read and update data in Firestore using Twilio Functions?有什么方法可以使用 Twilio 函数读取和更新 Firestore 中的数据?
【发布时间】:2021-09-23 10:35:40
【问题描述】:

我正在尝试使用以下代码从 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

【问题讨论】:

  • “似乎什么都没有运行”是什么意思?另外,你能取悦你的依赖文件吗?您可以查看How to create a Minimal, Reproducible Example 帮助部分以获取有关最小可重现代码的一些提示,不幸的是,您的 sn-p 不包含某人重现它所需的所有内容。除了提供这两个文件之外,您还应该提供运行它的步骤。我不熟悉 firebase,但我可以看到这是一个“魔术”handler 导出,应该由您的框架/库调用。
  • “似乎什么都没有运行”,我的意思是处理程序中的 console.log() 语句不会将任何内容记录到控制台。只有最后的行发送响应,即“Hello World”

标签: node.js database firebase twilio serverless


【解决方案1】:

这里是 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);
};

【讨论】:

  • 非常感谢!另外,为什么console.log() 不起作用?它仍然无法正常工作。我在日志语句之后添加了twiml.say() 语句,然后我能够看到正在执行哪些条件。关于如何解决这个问题的任何想法?
  • 您在哪里寻找console.logs?您是通过 Twilio 控制台中的 UI 还是通过 Twilio CLI 使用 Twilio 函数?
  • 我正在通过 UI 使用 Twilio 函数。我检查了错误日志以及浏览器控制台。
  • 您需要检查实时日志是否打开,在代码区域下,然后您应该会看到它们。
猜你喜欢
  • 2019-05-28
  • 2020-12-24
  • 2017-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多