Hello as the title says I have some problems when it comes to accessing an array of a query. The query is done like so:
async getTrack(id, type: String) : Promise<Track[]> {
if(type=='id'){
const query = this.trackModel.find(
{ id : id },
{_id:0}
).limit(100);
return query;
}
if(type=='name'){
const query = this.trackModel.find(
{ name : id },
{_id:0}
).limit(100);
return query;
}
}
And then calling the query like so: let json = await this.trackService.getTrack(id, type);
Printing the query works fine and the “artist_name” array shows up just fine. However doing console.log(json[0].artist_name)
prints undefined but printing any other attributes(that are not arrays) works fine.
The find() method returns a FindCursor that manages the results of your query. You can iterate through the matching documents using one of the following cursor methods:
next()
toArray()
forEach()
If no documents match the query, find() returns an empty cursor
weirdly enough it does not let me access the toArray() function as it says “Property ‘toArray’ does not exist on type ‘Query<(Track & { _id: ObjectId; }), Track & { _id: ObjectId; }, {}, Track>’.”
Ok if anyone else ever has this problem check the way you defined your schema. In my case the MongoDB field was “id_artist” and I was trying to access “artist_id” small typo but still.