• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

270
Vistas
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 3 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

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 3 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda