question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dialogflow fulfillment is not working with Mongoose to connect to MongoDB

See original GitHub issue

I wrote a basic function that connects to MongoDB using Mongoose, reads documents, and deployed the code in Cloud Functions. It responded correctly with the documents from MongoDB.

Code:

var mongoose = require('mongoose');
var mongoDB = "mongodb://IP:port/db";
mongoose.connect(mongoDB, {
    useNewUrlParser: true
});
var db = mongoose.connection;
var Schema = mongoose.Schema;
var myCollection = new Schema({name: String,PO: String},{collections: 'myCollection'});
var myCollectionModel = mongoose.model('myCollection', myCollection, 'myCollection');

exports.helloWorld = (req, res) => {
  db.on('error', console.error.bind(console, 'connection error:'));
  myCollectionModel.find({name : "Jeeva"}, function(error, PO) {
      res.send(PO[0]);
  });
};

I wrote the code to accommodate in Dialogflow fulfillment in the following directions: When the intent is called, fetch the data, return it to the user.

Code:

'use strict';
const admin = require('firebase-admin');
const functions = require('firebase-functions');

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

var mongoose = require('mongoose');
var mongoDB = "mongodb://IP:Port/db";
mongoose.connect(mongoDB, {useNewUrlParser: true});
var db = mongoose.connection;
var db = mongoose.connection;
var Schema = mongoose.Schema;
var myCollection = new Schema({name: String,PO: String},{collections: 'myCollection'});
var myCollectionModel = mongoose.model('myCollection', myCollection, 'myCollection');

process.env.DEBUG = 'dialogflow:debug';
 
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));
 
  function getData(name){
    var PO_number;
    myCollectionModel.find({name : "Jeeva"}, function(error, PO) {
      PO_number = PO[0].PO;
	});
    return PO_number;
  }
  
  function pending(agent){
    var name = agent.parameters.person;
    try{
      var PO_number = getData(name);
      agent.add(PO_number);
    }catch(error){
      agent.add(error.toString()); // Error: Unknown response type: "undefined"
    }
  }
    
  let intentMap = new Map();
  intentMap.set('pending-PO',pending);
  agent.handleRequest(intentMap);
});

It is not working. Does Dialogflow Fulfillment only support Firestore?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
JeevaTMcommented, Aug 23, 2019

Hi this is not a Issue with dialogflow itself. You need to set promises to work with it. And the object db in this sample you just instantiate it twice var db = … You can use the firebase dialogflow as example on how’s you promises should look like at dialogflow fullfitment objects.

Thank you for the information.

Information I have gathered why it did not work and what changes has to be made in code:

  • myCollectionModel.find() function runs asynchronously.
  • Dialogflow library requires you to use JavaScript Promises if you’re doing any asynchronous operation
function getData(name){
    var PO_number;
    return myCollectionModel.find({name : name}).exec()
      .then( doc => {
        return Promise.resolve(doc[0].PO);
      });
  }

  function pending(agent){
    var name = agent.parameters.person;
    return getData(name)
      .then( PO_number => {
      	agent.add(name + ", approval pending for following PO");
        agent.add( PO_number );
      })
      .catch( error => {
        agent.add("There no pending PO for approval, " + name + ".");
      });
  }
0reactions
cristiaan05commented, Sep 10, 2019

I already did this buy i don’t know how proof if it is connected or not

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dialogflow fulfillment is not working with Mongoose to connect ...
I wrote a basic function that connects to MongoDB using Mongoose, and reads documents. I deployed the code in Cloud Functions. It responded ......
Read more >
[Solved]-Mongoose.connect not working-mongodb
Your mongoose.connect call is working fine. If it wasn't then you would definitely have received a PromiseRejection for connection failure and a deprecated ......
Read more >
How to make a Webhook for Dialogflow Fulfillment - Medium
We would use Mongo as DB, and for creating the same, go to mLab and sign up for free. After logging in, click...
Read more >
Build fulfillment with the Actions on Google Node.js client ...
You can use the client library in conjunction with the Dialogflow ... For example, if you are using Cloud Functions for Firebase, ...
Read more >
Integrate the MongoDB API with the Google Dialogflow API
Setup the MongoDB API trigger to run a workflow which integrates with the Google Dialogflow API. Pipedream's integration platform allows you to integrate ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found