问题描述
我正在尝试使用以下代码从 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 函数视为异步函数。事实证明,大多数都是同步的,但是当您在 .get
上调用 userRef
时,这是一个异步调用。由于您正在使用 try/catch
,我们可以通过将整个 Twilio 函数定义为 async
函数来快速解决此问题。
exports.handler = async function(context,event,callback) {
然后您可以在 await
之前使用 userRef.get()
,事情应该开始工作了。
const doc = await userRef.get();
第二个问题是您进行了异步调用,而不是在最终调用 callback
之前等待响应。一旦您调用 callback
函数,其余的函数执行将终止。因此,虽然实际上对 userRef.get()
进行了异步调用,但一旦函数到达 callback
,它就会被取消,因为您没有在等待结果。
在这两种情况下,添加 async
和 await
应该可以解决问题,您的函数应该会按预期运行。
这是添加了 async
和 await
的整个函数:
var firebase = require('firebase');
exports.handler = async function(context,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);
};