无法从消防仓库集合中读取



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

1(从代理获取数据:

name=agent.parameters.name;

location=agent.parameters.location;

2(写信给消防仓库集合用户

db.collection("Users"(.doc("101"(.set({name:名称,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);
})
'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);
});

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

此外,您的回复"用户已插入"是在您没有实际确认用户文档已设置的情况下发送的。甚至不能保证它在函数完成时已经发送,因为您没有将文档编写视为异步。

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

相关内容

  • 没有找到相关文章

最新更新