【问题标题】:Unable to read from firestore collection无法从 Firestore 集合中读取
【发布时间】:2019-04-30 04:48:46
【问题描述】:

我有一个名为 Users 的 Firestore 集合,并试图通过履行在集合中写入一个文档。我正在通过对话流代理获取用户的姓名和他的位置,并尝试将其插入到集合中。但是文档没有被插入。

1)从代理获取数据:

name=agent.parameters.name;

location=agent.parameters.location;

2) 写入 Firestore 集合用户

db.collection("用户").doc("101").set({ 姓名:姓名, 位置:位置});

函数已执行,但文档未插入到 firestore 集合中。我缺少什么?

'use strict';

const functions = require('firebase-functions');
const { WebhookClient } = require('dialogflow-fulfillment');
const { Card, Suggestion } = require('dialogflow-fulfillment');
const admin = require('firebase-admin');

process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements


exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
 const agent = new WebhookClient({ request, response });
 console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
 console.log('Dialogflow Request body: ' + JSON.stringify(request.body));


var name='';
var location='';

admin.initializeApp(functions.config().firebase);
const db = admin.firestore();

 function getUserDetails(agent)
 {
     name= agent.parameters.name;
     location=agent.parameters.location;
     console.log("buyer name is " + name);
     db.collection("Users").doc("101").set({
    name: name,
    location:location});
    agent.add(`User has been inserted`);   
 }

 intentMap.set('Buy Car', getUserDetails);
 agent.handleRequest(intentMap);
})

【问题讨论】:

  • 您能否更新您的问题以澄清问题 - 当您尝试此操作时会发生什么?您是否收到错误消息,或者只是文档未存储?您还谈到“从”集合中“读取”——您是在尝试取回数据,还是现在只是尝试写入数据?
  • 更新了问题...

标签: google-cloud-firestore dialogflow-es fulfillment


【解决方案1】:

目前尚不清楚出了什么问题,但至少部分原因是您没有检查写入集合/文档时可能出现的错误。

您的回复“已插入用户”在您没有实际确认用户文档已设置的情况下发送,这使情况更加复杂。甚至无法保证在函数完成时它已经发送,因为您没有将文档写入视为异步。

执行此操作的正常方法是返回 set() 返回的 Promise,因此 handleRequest() 将在发送回复之前等待处理程序完成。您还应该在 Promise 的 then() 部分设置回复,并在 catch() 块中捕获任何错误。

这样的函数更正确,并且可以记录错误是什么:

 function getUserDetails(agent)
 {
     name= agent.parameters.name;
     location=agent.parameters.location;
     console.log("buyer name is " + name);
     return db.collection("Users").doc("101").set({
       name: name,
       location: location
     })
     .then( () => {
       agent.add(`User has been inserted`);   
     })
     .catch( err => {
       console.log( err );
       agent.add('Something went wrong');
     });
 }

【讨论】:

    【解决方案2】:
    'use strict';
    
    const functions = require('firebase-functions');
    const admin = require('firebase-admin');
    const {WebhookClient} = require('dialogflow-fulfillment');
    
    process.env.DEBUG = 'dialogflow:*'; // enables lib debugging statements
    admin.initializeApp(functions.config().firebase);
    const db = admin.firestore();
    exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => 
     {const agent = new WebhookClient({request,response});
      function saveName(agent){
        const nameParam = agent.parameters.name;
        const name = nameParam;
        agent.add(`thank you, ` + name + `!`);
    
      return db.collection('test').add({name: name}).then((snapshot) => {(console.log('success'));
        });
      }
    
      let intentMap = new Map();
      intentMap.set('Get_Name', saveName);
      agent.handleRequest(intentMap);
     });
    

    【讨论】:

    • 代码获取用户输入的名称,然后将其发送到名为 test 的数据库集合中,然后显示用户的输入.....但在您的情况下,您可能需要指定您希望数据存储在哪个文档中...希望您理解
    猜你喜欢
    • 2021-06-30
    • 2020-02-03
    • 1970-01-01
    • 2021-03-01
    • 2020-07-05
    • 2015-02-11
    • 1970-01-01
    • 2021-05-07
    相关资源
    最近更新 更多