J
J
juventino962018-03-24 11:00:42
MongoDB
juventino96, 2018-03-24 11:00:42

Write result from mongodb query to variable?

How to write result from mongodb query to variable.

var user = db.collection( config.db.collections.user ).findOne({ 'email' : email }, function(error, user) {
            if (error) throw error;

            client.close();
        });

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
eternalSt, 2018-03-24
@eternalSt

Good day!
Judging by the question and the provided code, you have no understanding of how asynchronous functions work in a node. In this answer , I already gave guidance on understanding asynchrony.
Now regarding the question.
If the code is written correctly, then the result is already stored in a variable, in this case, in the user
PS variable.
If you don't like callbacks, then use the ESNext goodies. mongodb native driver v3.0 ECMAScript Next CRUD

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question