I'm trying to retrieve all of the object values from an mongoose schema for example:
{
id: 553a75a7e4b092e5edad4bce,
name: Jeff,
name1: Ricky,
name2: Lexi,
name3: Eric
} I'm expecting Jeff Ricky Lexi Eric
I used the following:
{
Name.find(function(err, s){
if (err){
console.log(err);
}
else{
mongoose.connection.close();
Object.values(s).forEach(val=>{
console.log(val));
});
}
});
}
But I got this instead:
{
id: 553a75a7e4b092e5edad4bce,
name: Jeff,
name1: Ricky,
name2: Lexi,
name3: Eric
}
Any solutions for this?
Mongoose find method is not returning a single object but an array of objects.
If you wany to display values from them you can try using nested loops. Something like this should work
{
Name.find(function(err, s){
if (err){
console.log(err);
}
else{
mongoose.connection.close();
s.forEach(obj => {
Object.values(obj).forEach(val=>{
console.log(val));
});
});
}
});
}
Just keep in mind that you would display every property of all returned objects.