【问题标题】:Firebase Firestore - unable to get the value of a field in a collectionFirebase Firestore - 无法获取集合中字段的值
【发布时间】:2018-02-26 14:00:35
【问题描述】:

我有一个如图所示定义的 Firebase Firestore

字段 zaaidata 是一个对象。我想在 Dialogflow 中发送给我的聊天机器人的响应中使用 totvan 的值一个网络钩子。

我设法查询了文档,但我无法检索特定值。我已经试过了:

  • 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);
        }); 
});

有人可以帮我解决这个问题吗?谢谢!

【问题讨论】:

标签: javascript firebase google-cloud-firestore


【解决方案1】:

你试过了吗

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

【讨论】:

    猜你喜欢
    • 2020-03-15
    • 2021-05-07
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 2020-09-10
    • 1970-01-01
    • 2018-03-24
    • 1970-01-01
    相关资源
    最近更新 更多