I am making AWS Lex bot using lambda function and trying to connecting it to database of MongoDB Atlas using "mongoose" and database of firebase using "cloud firestore".But no data is saving to the database and even there is no error giving at cloud watch and not a single console is showing there
I am runnning the following command to deploy lambda function on AWS Lex bot in both cases of mongodb Atlas and Cloud Firestore:
$ aws lambda update-function-code --function-name Hotel-Booking-Agent --zip-file fileb://index.zip
Below is the code for connecting mongodb atlas with lambda function
'use strict';
var mongoose = require('mongoose');
var dbURI = "mongodb+srv://author:******@cluster0-geoiq.mongodb.net/test?retryWrites=true";
mongoose.connect(dbURI, { useNewUrlParser: true })
.catch((e) => {
console.log("catch error: ", e)
})
mongoose.connection.on('error', function (err) {//any error
console.log('Mongoose connection error: ', err);
process.exit(1);
})
mongoose.connection.on('connected', function () {//connected
console.log("Mongoose is connected");
})
mongoose.connection.on('disconnected', function () {//disconnected
console.log("Mongoose is disconnected");
process.exit(1);
});
console.log("reach here")
var userSchema = new mongoose.Schema({
"name": { type: String, required: true },
"email": { type: String, required: true },
"Detail": { type: String, required: true }
},
{
collection: "user1"
});
exports.handler = (event, context, callback) => {
try {
console.log(`request received for userId=${event.userId}, intentName=${event.currentIntent.name}`);
var slots = event.currentIntent.slots;
var noOfPeople = slots.noOfPeople;
var noOfRoom = slots.noOfRoom;
var roomKind = slots.roomKind;
var subject = slots.subject;
var name = slots.name;
var email = slots.email;
switch (event.currentIntent.name) {
case 'Hotel_Booking':
var userModel = mongoose.model("user", userSchema);
var newUser = new userModel({
"name": name,
"email": email,
'Detail': `You have booked ${noOfRoom} rooms for ${noOfPeople} people in ${roomKind} category`
})
newUser.save((dataSaved) => {
console.log("datasaved", dataSaved)
})
context.succeed({
"dialogAction": {
"type": "Close",
"fulfillmentState": "Fulfilled",
"message": {
'contentType': 'PlainText',
'content': `Okay, ${name} I have book ${noOfRoom} rooms for ${noOfPeople} people in ${roomKind} category`
}
}
})
break
default:
context.succeed({
"dialogAction": {
"type": "Close",
"fulfillmentState": "Fulfilled",
"message": {
'contentType': 'PlainText',
'content': `Can't understand`
}
}
})
}
}
catch (err) {
context.succeed(err);
}
};
The data must be saved on database as in the code.Also that AWS Lex bot is giving a response as directed in lambda function but there is no data showing in database of MongoDB Atlas. Snap of mongodb Atlas Database
Here is the code for connecting firestore with lambda function
'use strict';
var admin = require("firebase-admin");
var serviceAccount = require("./agent-e2634-firebase-adminsdk-0n8na-40c5078975.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://agent-e2634.firebaseio.com"
});
const db = admin.firestore();
exports.handler = (event, context, callback) => {
try {
console.log(`request received for userId=${event.userId}, intentName=${event.currentIntent.name}`);
var slots = event.currentIntent.slots;
var noOfPeople = slots.noOfPeople;
var noOfRoom = slots.noOfRoom;
var roomKind = slots.roomKind;
var subject = slots.subject;
var name = slots.name;
var email = slots.email;
switch (event.currentIntent.name) {
case 'Hotel_Booking':
var docRef = db.collection('users').doc('info');
var setAda = docRef.set({
'name': name,
'email': email,
'content': `Okay, ${name} I have book ${noOfRoom} rooms for ${noOfPeople} people in ${roomKind} category`
});
db.collection('users').get()
.then((data) => {
context.succeed({
"dialogAction": {
"type": "Close",
"fulfillmentState": "Fulfilled",
"message": {
'contentType': 'PlainText',
'content': `Okay, ${name} I have book ${noOfRoom} rooms for ${noOfPeople} people in ${roomKind} category`
}
}
})
console.log(doc.id, '=>', data);
})
.catch((err) => {
console.log('Error getting documents', err);
});
break
default:
context.succeed({
"dialogAction": {
"type": "Close",
"fulfillmentState": "Fulfilled",
"message": {
'contentType': 'PlainText',
'content': `Can't understand`
}
}
})
}
}
catch (err) {
context.succeed(err);
}
}
When i deploy the above code the bot returns the following error:
An error has occurred: Invalid Lambda Response: Received error response from Lambda: Unhandled
the same code is working fine without connecting to database and even no collection and docs are making on cloud firestore.