我有一个如图所示的Firebase Firestore
javascript - Firebase Firestore-无法获取集合中字段的值-LMLPHP

zaaidata字段是一个对象。我想在通过webhook发送到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);
        });
});


有人可以帮我吗?谢谢!

最佳答案

你尝试过这个吗

response = "Je kan zaaien tussen " + doc.data().zaaidata.van + " en " + doc.data().zaaidata.tot;

08-18 23:29