简体   繁体   English

Firebase Firestore-无法获取集合中字段的值

[英]Firebase Firestore - unable to get the value of a field in a collection

I have a Firebase Firestore defined as shown in the image 我有一个如图所示的Firebase Firestore 在此处输入图片说明

The field zaaidata is an object. zaaidata字段是一个对象。 I want to use the value of tot and van in a response that is sent to my chatbot in Dialogflow with a webhook. 我想在通过webhook发送到Dialogflow中我的聊天机器人的响应中使用totvan的值。

I managed to query the document, but I am unable to retrieve the specific values. 我设法查询了文档,但是无法检索特定值。 I already tried this: 我已经试过了:

  • doc.data["zaaidata"]["van"] doc.data [“ zaaidata”] [“ van”]
  • doc.data("zaaidata")("van") doc.data(“ zaaidata”)(“ van”)
  • doc.data.get("zaaidata")["van"] doc.data.get(“ zaaidata”)[“ van”]

Here is my index.js 这是我的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);
        }); 
});

Can someone help me on this, please? 有人可以帮我吗? Thanks! 谢谢!

你尝试过这个吗

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 firebase firestore 的集合中搜索字段 - Search for a field in a collection on firebase firestore Firebase Firestore:如何更新或访问和更新 map、数组、文档、集合中的字段值 - Firebase Firestore: How to update or access and update a field value, in a map, in an array, in a document, that is in a collection Firebase Firestore获取集合中每个文档的子集合 - Firebase Firestore Get Subcollections for each document in a collection Firebase Firestore 集合获取方法导致错误 - Firebase Firestore collection get method causing error Firestore:将值添加到集合中,并将另一个值添加到该新集合的字段中 - Firestore: Adding a value into a collection and another value into that new collection's field 无法从 Firebase Firestore 获取字段 - Can't get field from Firebase Firestore 有没有办法查询 Firestore 集合以获取字段值出现的次数? - Is there a way to query for a Firestore collection to get the number of times a field's value occurs? firebase.firestore().collection().where(_, _, *) 'value' 参数预期 - firebase.firestore().collection().where(_, _, *) 'value' argument expected 如何在 Firebase Cloud Firestore 中删除具有相同键值的集合中的文档 - How to delete documents in a collection with the same key value in Firebase Cloud Firestore 从Firebase Firestore参考获取异步值 - get asynchronous value from firebase firestore reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM