Ajay Srikanth
Ajay Srikanth

Reputation: 1185

mongoose find not fetching any result

I've simple collection in mongo and a corresponding mongoose model. This collection will only contain one document always. When I run a query in mongoshell it is giving me the result, but when I try to do findOne using mongoose it is not returning any result at all. Can someone help me figure out what is wrong. Below is my code.

Model:

const mongoose = require('mongoose');
const schema = new mongoose.Schema({
    lastResetMonth: {
        type: Number
    },
    lastResetWeek: {
        type: Number
    },
    currentFisYear:{
        type: Number
    }
});
module.exports = mongoose.model('ReserveResetTrack', schema, 'reserveResetTrack');


const ReserveResetTrack = require('../models/ReserveResetTrack');

ReserveResetTrack.findOne({})
        .then(trackData => {
            return {
                lastFisMonth: trackData.lastMonth,
                lastFisWeek: trackData.lastWeek
            }
        });

The above code is always returning nothing but a promise.

This is the only document i've in my collection and this will be the only one for ever

{
    "_id" : ObjectId("589271a36bfa2da821b13ce8"),
    "lastMonth" : 0,
    "lastWeek" : 0,
    "currentYear" : 0
}

Upvotes: 1

Views: 555

Answers (1)

Santanu Biswas
Santanu Biswas

Reputation: 4787

Use exec() like this:

ReserveResetTrack.findOne({})
    .exec()   // <--- use exec() here
    .then(trackData => {
        return {
            lastFisMonth: trackData.lastMonth,
            lastFisWeek: trackData.lastWeek
        }
    });

Upvotes: 2

Related Questions