1

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
}
Ajay Srikanth
  • 1,095
  • 4
  • 22
  • 43

1 Answers1

2

Use exec() like this:

ReserveResetTrack.findOne({})
    .exec()   // <--- use exec() here
    .then(trackData => {
        return {
            lastFisMonth: trackData.lastMonth,
            lastFisWeek: trackData.lastWeek
        }
    });
Santanu Biswas
  • 4,699
  • 2
  • 22
  • 21
  • what is the use of it? Why should I do that, where as it is working fine for my other models. Could you please explain. – Ajay Srikanth Feb 02 '17 at 16:58
  • I've tried the exec and it didn't work as well unfortunately. Also, as per the answer below it should get executed when you use promise. http://stackoverflow.com/questions/31549857/mongoose-what-does-the-exec-function-do – Ajay Srikanth Feb 02 '17 at 17:04
  • Mongoose queries like `findOne()` are not Promise. It is a query. The queries have a `.then()` function but it is for yield and async/await. The `exec()` method of a query returns a Promise. See docs here for more info [http://mongoosejs.com/docs/promises.html](http://mongoosejs.com/docs/promises.html) – Santanu Biswas Feb 02 '17 at 17:04
  • In the link provided by you see the 'Update' part of the accepted answer. It clearly says **Something to note when using Promises in combination with Mongoose async operations is that Mongoose queries are not Promises. Queries do return a thenable, but if you need a real Promise you should use the exec method. More information can be found here.** Please note that the `then` method of query is for yield and async/await. It is not a Promise. – Santanu Biswas Feb 02 '17 at 17:08
  • Yes you are right, though it is not a full fledged promise unless you use exec, my code should execute the query without the exec and then part should get executed right. I don't that happening by either ways. Do you see any thing else wrong in my code. – Ajay Srikanth Feb 02 '17 at 17:33
  • Marking this as accepted because it helped me understand how to get the promise from the query which I've used to join with my other promise that is being resolved before executing this query. Thanks for you help Santanu. – Ajay Srikanth Feb 02 '17 at 17:45