【发布时间】:2019-09-15 12:22:46
【问题描述】:
我目前在我的节点后端调用一个带有对话框流的实现 webhook,在 firestore 数据库上执行 crud 操作。有没有更好、更简洁的方法来编写这些?
我的代码看起来写得很糟糕,但它确实有效。我正在努力编写更清晰、更易读的代码,因此我正在寻找有人给我一些关于如何使用 webhook 编写更好的 API 调用的指导。
//DATABASE API CALLS HERE!//
case "FAV_COLOR":
agent.handleRequest(agent => {
return new Promise(() => {
async function writeToDb() {
// Get parameter from Dialogflow with the string to add to the database doc
const databaseEntry = agent.parameters.color;
// Get the database collection 'user' and document 'color' and store
// the document {entry: "<value of database entry>"} in the 'color' document
const dialogflowAgentRef = db.collection("user").doc("color");
try {
await db.runTransaction(transaction => {
transaction.set(dialogflowAgentRef, {
entry: databaseEntry
});
return Promise.resolve("Write complete");
});
agent.add(
`Wrote "${databaseEntry}" to the Firestore database.`
);
} catch (e) {
agent.add(
`Failed to write "${databaseEntry}" to the Firestore database.`
);
}
}
writeToDb();
});
});
break;
default:
console.log("ITS BROKEN");
它目前在 switch 语句中,因为我想根据操作触发不同的实现。两个 agent.add 语句都不会触发。
另外,如果有人能提供一些关于调试这些的技巧,我将不胜感激。我刚刚部署了这些功能,添加了一个 console.log(JSON.stringify());然后检查 firebase 控制台功能部分是否有错误。看起来非常低效。
感谢您花时间回答:)
千斤顶
【问题讨论】:
-
我投票结束这个问题,因为它要求同行评审以改进工作代码。它更适合专门为此目的创建的Code Review。该网站是针对涉及无法正常工作的代码的问题。
-
非常感谢肯,我会把问题移到那里。
标签: node.js firebase google-cloud-firestore dialogflow-es chatbot