Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

130
Visualizações
variable undefined after findOne operation mongodb

I am trying to make an API that makes use of 2 databases to generate a fine. Here is the code:

router.get("/generateFine/:bookingID/:currDate", function (req, res, next) {
    var currDate,
        returnDate,
        fine,
        totalFine = 0;
    Booking.findOne({ _id: req.params.bookingID }).then(function (booking) {
        Car.findOne({ _id: booking.carID }).then(function (car) {
            currDate = Date.parse(req.params.currDate) / 1000 / 3600 / 24;
            returnDate = Date.parse(booking.bookingDates[1]) / 1000 / 3600 / 24;
            fine = car.fine;
            if (currDate > returnDate) {
                totalFine = fine * (currDate - returnDate);
            }
            console.log(totalFine);
            // res.send(totalFine);
        });
        console.log("totalFine is " + totalFine);
        // res.send(totalFine);
    });
});

Here are the two Schemas used in the code: Booking Schema:

{
    "_id" : ObjectId("621bf46602edf12942f0d5c9"),
    "carID" : "621b87af70c150da70b1dabf",
    "bookingDates" : [ 
        "2022-03-05", 
        "2022-03-06"
    ],
}

Car Schema:

{
    "_id" : ObjectId("621b87af70c150da70b1dabf"),
    "name" : "Toyota",
    "rate" : 60,
    "fine" : 10,
    "datesBooked" : [ 
        {
            "from" : "2022-03-05",
            "to" : "2022-03-06"
        }, 
        {
            "from" : "2022-03-07",
            "to" : "2022-03-08"
        }, 
        {
            "from" : "2022-03-09",
            "to" : "2022-03-10"
        }
    ],
    "__v" : 0
}

I want to return the generated fine to the user. When I am trying to send the result, it throwing an error. The first console log prints the correct result, but the second console log prints 0. Also, how can I send the result without getting an error. Thanks already!

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You could use $lookup aggregation pipeline stage to include the car document that matches on the carID field, create additional computed fields that will aid you in getting the total fine whilst using the necessary aggregation operators.

Essentially you would need to run an aggregate pipeline that follows:

const mongoose = require('mongoose');

router.get('/generateFine/:bookingID/:currDate', async function (req, res, next) {
    const currDate = new Date(req.params.currDate);
    const [{ totalFine }] = await Booking.aggregate([
        { $match: { _id: mongoose.Types.ObjectId(req.params.bookingID) }},
        { $lookup: {
            from: 'cars', // or from: Car.collection.name
            let: { carId: { $toObjectId: '$carID' } }, // convert the carID string field to ObjectId for the match to work correctly
            pipeline: [ 
                { $match: {
                    $expr: { $eq: [ '$_id', '$$carId' ] }
                } }
            ],
            as: 'car'
        } },
        { $addFields: { 
            car: { $arrayElemAt: ['$car', 0 ] }, // get the car document from the array returned above
            returnDate: {
                $toDate: { $arrayElemAt: ['$bookingDates', 1 ]}
            }
        } },
        // compute the overdue days 
        { $addFields: { 
            overdueDays: {
                $trunc: {
                    $ceil: {
                        $abs: {
                            $sum: { 
                                $divide: [
                                    { $subtract: [currDate, '$returnDate'] },
                                    60 * 1000 * 60 * 24
                                ] 
                            }
                        }
                    }
                }
            }
        } },
        { $project: { // project a new field
            totalFine: { 
                $cond: [
                    { $gt: [currDate, '$returnDate'] }, // IF current date is greater than return date
                    { $multiply: ['$car.fine',  '$overdueDays'] }, // THEN multiply car fine with the overdue days
                    0 // ELSE total fine is 0
                ]
            }
        } }
    ]).exec();

    console.log("totalFine is " + totalFine);
    // res.send(totalFine);
});
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda