无法从Firestore集合中读取

时间:2018-11-28 10:00:36

标签: google-cloud-firestore dialogflow fulfillment

我有一个名为“用户”的Firestore集合,并尝试通过实现在集合中编写文档。我正在通过dialogflow代理获取用户名和位置,并尝试将其插入集合中。但是没有插入文档。

1)从代理获取数据:

name = agent.parameters.name;

location = agent.parameters.location;

2)写入Firestore集合用户

db.collection(“ Users”)。doc(“ 101”)。set({         名称:名称,         location:location});

函数已执行,但文档未插入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);
})

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

在您未真正确认已设置用户文档的情况下,发送了“已插入用户”答复,这使情况更加复杂。甚至不能保证在函数完成时就已经发送了它,因为您没有将文档写入视为异步的。

执行此操作的通常方法是返回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');
     });
 }