0

Ok, so this is my first question here (but I have been here many times, always finding the right answer to my problems). Anyway, let's get to the point:

I am fairly new to Node and Mongoose and I have the following issue:

I am making a query to Mongoose that looks like:

myProblem() {
    const channels = DreamProperties.find({
        'status': 'ON',
        'updatedAt': { $exists: true }
    }, {
        '_id': 0,
        'channel.channelName': 1,
        'updatedAt': 1
    }, (error, result) => {
        if (error) return error;
    })
    console.log(channels);

    return channels;
}

This works fine, it returns the desired fields (updatedAT comes from the built-in timestamps in Mongoose). HOWEVER, when i try to access the field(I want to build another object with the correct key names I need), after it is returned- it is undefined.

Promise.all([
        0 PROMISE
        1 PROMISE
        2 PROMISE
        data.myProblem()
    ]).then(promiseData => {
        let result = {};
        for (var i = 0; i < promiseData[3].length; i++) {
            result = {
                'name': promiseData[3][i].channel.channelName,
                'y': promiseData[3][i]
            }
        }
        console.log(result);
        console.log(promiseData[3][2].updatedAt); // returns undefined
        console.log(promiseData[3][2].channel); //returns desired result

I don't know if my question becomes clear by the snippets, so tell me if I need to make clarifications.

  • How does your schema look like? Have you checked this http://stackoverflow.com/questions/22139113/mongoose-returns-undefined-for-an-existing-field ? – Gustavo Straube Apr 10 '17 at 12:46
  • I was searching in a whole other direction, so I haven't seen this question. It solved my problem. Thank you for the fast and precise response. – Manol Denev Apr 10 '17 at 12:57

0 Answers0