I'm currently working on an API for my chatbot that saves user input to my database. It works fine when I do queries and it saves to my database without any problems. My problem is that it should send a response that it got the user query.
Here's my code for the route/controller:
if (req.body.queryResult.parameters.informationType == "appointment") {
const user = {
firstName: req.body.queryResult.parameters.firstName,
lastName: req.body.queryResult.parameters.lastName,
mobile: req.body.queryResult.parameters.mobile,
email: req.body.queryResult.parameters.email,
status: "Pending",
informationType: req.body.queryResult.parameters.informationType,
appointmentDate: dateToISO(req.body.queryResult.parameters.appointmentDate),
};
db.db()
.collection("users")
.insertOne(user, (err, result) => {
if (err) {
return res.json({
"fulfillmentText": "Error has occured. Cannot add user.",
});
} else {
var firstName = result.ops[0].firstName;
var lastName = result.ops[0].lastName;
return res.json({
"fulfillmentText":
"Okay, got it " +
firstName +
" " +
lastName +
". We will send an email or text message from the contact info you have provided once your appointment has been approved.",
});
}
})
}
Also, here's its model:
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var user = new Schema({
firstName: {
type: String,
required: true,
},
lastName: {
type: String,
required: true,
},
mobile: {
type: String,
required: true,
},
email: {
type: String,
required: true,
},
status: {
type: String,
required: true,
},
appointmentDate: {
type: Date,
required: true,
},
informationType: {
type: String,
required: true,
},
});
module.exports = mongoose.model("users", user);
Okay, I stumbled upon this post and read some documentation. Apparently, the approach that I used doesn't work anymore.
After hours of trying and scouring various post, I finally found the solution (which was actually a no-brainer). This is what I did:
var firstName = req.body.queryResult.parameters.firstName;
var lastName = req.body.queryResult.parameters.lastName;