Shobana VK
Shobana VK

Reputation: 389

Unable to read from firestore collection

i have a firestore collection called Users and trying to write a document in the collection through fulfillment. I am getting the user's name and his location through dialogflow agent and trying to insert it into the collection. But the document is not getting inserted.

1)Getting data from agent:

name= agent.parameters.name;

location=agent.parameters.location;

2) Writing to the firestore collection Users

db.collection("Users").doc("101").set({ name: name, location:location});

The function got executed but the document is not inserted into the firestore collection. what am i missing?

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

Upvotes: 0

Views: 645

Answers (2)

Sahmmie Fainy
Sahmmie Fainy

Reputation: 406

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

Upvotes: 1

Prisoner
Prisoner

Reputation: 50701

It isn't clear what is going wrong, but at least in part that is because you're not checking what the error might be from doing the write to the collection/doc.

This is compounded by your reply "User has been inserted" is sent without you actually confirming that the user document has been set. There isn't even any guarantee that it has been sent by the time the function completes, because you're not treating the document write as asynchronous.

The normal way to do this is to return the Promise that set() returns, so handleRequest() will wait for the handler to finish before it sends the reply. You should also set the reply in a then() portion of the Promise and catch any errors in a catch() block.

A function like this is more correct, and may log what the error is:

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

Upvotes: 0

Related Questions