Skip to content
Advertisement

Destructuring a mongoose document

Using mongoose in my project, I ran into a problem. I want to find all documents that have such a key and value pair role: USER. I can get a list of documents, but I cannot get the values of specific fields from it, no matter how I try.
Here is my code:

JavaScript

The request definitely gets the document, because console.log(users) lists the documents.

JavaScript

Judging by the documentation of the mongoose, I am doing everything right. It is also advised to cast a document into an object using toObject(), but mongoose does not find such a method for request
Моя схема:

JavaScript

Advertisement

Answer

It’s an array, so trying to get userName won’t work. You need to get the specific element. Try this:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement