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.