Imagine a scenario where a resolver retrieves quotes from various books stored in a mongo db. This is how the data is structured within the database!
getQuotes: async () => {
const getQuotes = await booksModel.find({}, "quotes");
console.log(getQuotes);
// #1
for (const element of getQuotes) {
const test = element.quotes;
console.log(test)
// #2
Object.values(test).forEach((val) => {
console.log(val);
// #3
return val;
});
}
},
The result closely resembles the expected data structure...
This is console.log #3!
{
book: 1,
page: 12,
excerpt: 'asaaaad',
comment: 'asd',
_id: 'N_YOT_Gms_fvAqQKL3Y23'
}
{
book: 1,
page: 12,
excerpt: 'sdfsdfsdfsdf',
comment: 'asd',
_id: 'QP5iLh3Gj2X8ZcSN7hoPF'
}
{
book: 2,
page: 12,
excerpt: 'asaasdasdaad',
comment: 'asd',
_id: '2kdkfgW6MERwGtvbXph9e'
}
This is console.log #2!
[
{
book: 1,
page: 12,
excerpt: 'asaaaad',
comment: 'asd',
_id: 'N_YOT_Gms_fvAqQKL3Y23'
},
{
book: 1,
page: 12,
excerpt: 'sdfsdfsdfsdf',
comment: 'asd',
_id: 'QP5iLh3Gj2X8ZcSN7hoPF'
}
]
[
{
book: 2,
page: 12,
excerpt: 'asaasdasdaad',
comment: 'asd',
_id: '2kdkfgW6MERwGtvbXph9e'
}
]
This is console.log #1!
[
{ _id: 'leO68YLuG_mG4KS491Kpz', quotes: [ [Object], [Object] ] },
{ _id: 'ymbUWql0sREAKuaVXZ_KY', quotes: [ [Object] ] }
]
I seem to be struggling with organizing the data into a neat array using forEach in my graphql resolver. Any suggestions on approaching this differently? It's been quite some time since I delved into this, so pardon any missteps; I gave it my best shot :p