【发布时间】:2018-02-26 14:00:35
【问题描述】:
我有一个如图所示定义的 Firebase Firestore
字段 zaaidata 是一个对象。我想在 Dialogflow 中发送给我的聊天机器人的响应中使用 tot 和 van 的值一个网络钩子。
我设法查询了文档,但我无法检索特定值。我已经试过了:
- doc.data["zaaidata"]["van"]
- doc.data("zaaidata")("van")
- doc.data.get("zaaidata")["van"]
这是我的 index.js
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
var db = admin.firestore();
exports.webhook = functions.https.onRequest((req, res) => {
let parameters = req.body.result.parameters;
var groentenRef = db.collection('groenten').doc(parameters.groente);
var query = groentenRef.get()
.then(doc => {
if (!doc.exists) {
response = "No such document!";
} else {
response = "Je kan zaaien tussen " + doc.data["zaaidata"]["van"] + " en " + doc.data["zaaidata"]["tot"];
}
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({ "speech": response, "displayText": response}));
})
.catch(err => {
console.log('Error getting documents', err);
});
});
有人可以帮我解决这个问题吗?谢谢!
【问题讨论】:
-
我很确定是
doc.data()来获取代表文档的 JSON 对象。所以doc.data().zaaidata.van会得到起始日期。见firebase.google.com/docs/firestore/query-data/get-data -
这是正确的。
JSON.stringify(doc.data())会将对象呈现为字符串。
标签: javascript firebase google-cloud-firestore