dialogflow / dialogflow-fulfillment-nodejs

Dialogflow agent fulfillment library supporting v1&v2, 8 platforms, and text, card, image, suggestion, custom responses
Apache License 2.0
598 stars 281 forks source link

Dialogflow fulfillment is not working with Mongoose to connect to MongoDB #268

Closed JeevaTM closed 5 years ago

JeevaTM commented 5 years ago

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?

alexedtionweb commented 5 years ago

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.

JeevaTM commented 5 years ago

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:

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 + ".");
      });
  }
cristiaan05 commented 5 years ago

Can I use mysql to connect to dialogflow fulfillment?

JeevaTM commented 5 years ago

@cristiaan05 Yes, you can. There is a module for mysql in NodeJS. Tutorial

Remember JS is asynchronous so you have to use the same method as I did.

You can also use this module.

cristiaan05 commented 5 years ago

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