Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

364
Views
TypeError: Error Cannot read property '0' of undefined when returning a response from chatbot

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);

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!