Accessing arrays from a query

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[]> {
    const query = this.trackModel.find(
    { id : id },
return query;
    const query = this.trackModel.find(
  { name : id },
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
prints undefined but printing any other attributes(that are not arrays) works fine.

I dont really know how reliable is this there.

Anyways, did you try:

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

I would try find(...).toArray( )[0]

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>’.”

Did you do it like this?

const result = await.getTrack(...)



From what I see in the docs it should also be { projection: { _id: 0 } } as well in place of { _id: 0 }

Yes,and again I have no problem on getting the normal output it’s just getting the array out of the result that is the problem.

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.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.