I have a webhook application where I have successfully retrieved data from Firebase database. But I need to incorporate MongoDB instead. This is the code so far.
'use strict';
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const mongoose = require('mongoose');
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
let uri = 'mongodb://dbAdynor:Adynor123!@testcluster-shard-00-00-x87dz.gcp.mongodb.net:27017,testcluster-shard-00-01-x87dz.gcp.mongodb.net:27017,testcluster-shard-00-02-x87dz.gcp.mongodb.net:27017/test?ssl=true&replicaSet=TestCluster-shard-0&authSource=admin&retryWrites=true';
let db;
mongoose.connect(uri,{
useMongoClient:true,
useNewUrlParser: true
});
let mdb = mongoose.connection;
mdb.on('error', console.error.bind(console, 'connection error:'));
mdb.once('open', function callback() {
// Create song schema
let dbSchema = mongoose.Schema({
decade: String,
artist: String,
song: String,
weeksAtOne: Number
});
db=mongoose.model('songs',dbSchema);
let admission = new db({
decade: '1970s',
artist: 'Debby Boone',
song: 'You Light Up My Life',
weeksAtOne: 10
});
});
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));
console.log("request.body.queryResult.parameters: ", request.body.queryResult.parameters);
var params = request.body.queryResult.parameters;
// var name = request.body.queryResult.parameters['myName'];
var intentMap = new Map();
// intentMap.set('Default Welcome Intent', welcome);
// intentMap.set('Default Fallback Intent', fallback);
agent.handleRequest(intentMap);
});
I am getting an error that says "Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module 'mongoose'
at Function.Module._resolveFilename (module.js:476:15)
at Function.Module._load (module.js:424:25)
at Module.require (module.js:504:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/user_code/index.js:5:18)
at Module._compile (module.js:577:32)
at Object.Module._extensions..js (module.js:586:10)
at Module.load (module.js:494:32)
at tryModuleLoad (module.js:453:12)
at Function.Module._load (module.js:445:3)
"
Any idea how to resolve this?
Follow the steps to connect MongoDB ( using Mongoose )to your Dialogflow. I am continuing with the code you provided.
Code
Firebase Logs
Google Assistant Output
Notes:
npm install mongoose --save
in your functions folder. This would solve problems such asCannot find module mongoose
.Hope that helps!