Different results doing what I think is the same thing ( Mongoose js )

Asked

Viewed 31 times

-1

I have that code

var questionscount = await Question.find({
        $or: [
            { titulo: { $regex: termo, $options: 'i' } },
            { corpo: { $regex: termo, $options: 'i' } }
        ]
    }).count()
console.log(questionscount)

this functions normally.

But because when I do:

var questions = await Question.find({
        $or: [
            { titulo: { $regex: termo, $options: 'i' } },
            { corpo: { $regex: termo, $options: 'i' } }
        ]
    }) 
console.log(questions.count())

that makes a mistake

questions.Count is not a Function

  • 1

    What Question.find returns? An object?

2 answers

1

The operator await invokes the method then and return to you the argument that would callback. It is semantic sugar to turn functional code into imperative code.

The equivalent of the first code without await would be:

Question
    .find({
        $or: [
            { titulo: { $regex: termo, $options: 'i' } },
            { corpo: { $regex: termo, $options: 'i' } }
        ]
    })
    .count()
    .then(questionscount => {
        console.log(questionscount);
    });

While the second would be:

Question
    .find({
        $or: [
            { titulo: { $regex: termo, $options: 'i' } },
            { corpo: { $regex: termo, $options: 'i' } }
        ]
    })
    .then(questions => {
        console.log(questions.count());
    });

As you can see, they are not equivalent. In the first code you create your query with two pipelines (find and count), while in the second you mount the query only with find, and have it executed, and then invoke count in the result, but the result does not have that method, the method belongs to the query, not to the result.

The equivalent would be to do:

var questionsQuery = Question.find({
    $or: [
        { titulo: { $regex: termo, $options: 'i' } },
        { corpo: { $regex: termo, $options: 'i' } }
    ]
});
var questionsCount = await questionsQuery.count();
console.log(questionsCount);
  • I copied exactly the same code you put here to see if it worked, and returned Typeerror , questions.Count is not a Function .

  • @Rafa0712, read carefully, the second example I posted here is to make a mistake on purpose, just like the code you posted gives error.

1

You are using 'await', that means your code will be executed asynchronously, that is, the rest of the code will be executed while waiting for the callback.

When you use:

console.log(questions.count())

Generates an error because the asynchronous call has not yet returned the result, so the 'questions' object is still empty, so the 'Count()' function does not yet exist in it.

In its first example the code works, because the function 'Count()' is included in the asynchronous call, and therefore will only be executed when the object is effectively returned by the query.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.