我正在使用lambda函数制作AWS Lex bot,并尝试使用“ mongoose”将其连接到MongoDB Atlas的数据库,并使用“ cloud firestore”将其连接到firebase的数据库。但是没有数据保存到数据库,甚至没有错误在云端观看,没有显示任何控制台。
我已经完成了在MongoDB Atlas上进行集群创建的整个过程以及云存储的过程。
我正在运行以下命令,以在mongodb Atlas和Cloud Firestore的两种情况下在AWS Lex机器人上部署lambda函数:
$ aws lambda update-function-code --function-name Hotel-Booking-Agent --zip文件fileb://index.zip
下面是使用lambda函数连接mongodb地图集的代码
'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);
}
};
Snap of Cloud Watch for both cases
数据必须按照代码中的方式保存在数据库中。此外,AWS Lex bot会按照lambda函数的指示给出响应,但MongoDB Atlas数据库中没有显示数据。 Snap of mongodb Atlas Database
这是将Firestore与lambda函数连接的代码
'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);
}
};
Snap of firebase cloud firestore 当我部署以上代码时,机器人会返回以下错误:
发生错误:Lambda响应无效:从Lambda收到错误响应:未处理
相同的代码在不连接数据库的情况下也能正常工作,甚至没有集合和文档都在云Firestore上制作。