Skip to content Skip to sidebar Skip to footer

Return Resolve Error In Node Function

Why wont usernametoid function return the acual id? cause im trying to send the result of the userdata as the return. In this case, i want to only send the userdata`s _id attribute

Solution 1:

Because you are not passing correctly the fetched user to the query.exec.

You need to do:

varUsers  = require('../models/users-model.js');

functionusernametoid(id) {
    returnnewPromise( function (resolve, reject) {
        Users.findOne({ username : id }).then( function(user){
          //If you use lodash you can do _.isNull(user)if(user == null){
            returnreject({error : 'User not found'});
          }

          user.exec(function(userdata, error) {
              if(userdata){
                returnresolve(userdata);
              } 
              if(error){
                 returnreject({error : 'Error while executing query'});
              }
           });
        });
    });
}

I don't really get why you are importing Users Model like that. I do not think Node will be able to fetch it like that.

And, you should require mongoose in your server.js

To catch the rejection you need the following code:

UserFactory.userNameToId(id).then( function(response){
  if(response.error){
    console.log('error '+response.error);
  }
  if(response){
    console.log('Got response '+response);
  }
});

Post a Comment for "Return Resolve Error In Node Function"