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