U
U
unclefeudorsnew2015-03-14 18:07:25
MongoDB
unclefeudorsnew, 2015-03-14 18:07:25

How to bind mongodb and class in Node.js?

I have a 'User' model like this:

User=new Schema({
   'login':string,
   'firstName':string,
   'lastName':string
})

Previously, when I wanted to get, for example, a username, I wrote something like:
User.findOne({'login':'peterjacobs123'}, function(err, user){
     //...error handle...
     console.log(user.firstName);
})

But suddenly I wanted to translate everything into OOP rails. And how to do this is still not very clear. At the output, I want to get a User class like this:
var user = new User('peterjacobs123');
console.log(user.firstName);

Immediately there is a problem with Mongodb asynchrony, and how to get around it is not clear. The only thing that comes to mind is to use promises, but exactly where to return them?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sasha, 2015-03-14
@madmages

mongoose

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question