2

I know this is a stupid question, but I'm missing something and I'm lost.

I have created a new project using 'yo meanjs' thus creating the standard scaffolding.

From the public side of the application I am trying to search the Users for a user based not on their id but rather their email address.

Some how I need to write the query using Users.get or Users.query but I cannot find an example of how to do this. Do I have to create new controllers/routes on the app side?

On the public side I've tried many different things like:

var user = Users.query({
    email: '[email protected]'
});

I can't seem to see the forest through the beautiful trees right now.

Thanks!

2 Answers 2

2

I guess the answer was a bit more complicated. This is working, although there may be a better way to do it.

I had to create a new function in one of the app user controllers:

exports.userByEmail = function(req, res, next, email) {
  console.log('userByEmail ' + email);
  User.findOne({
    email: email
  }).exec(function(err, user) {
    if (err) return next(err);
    if (!user) return next(new Error('Failed to load User with email ' + email));
    res.json(user);
    next();
  });
};

Next, I had to create a new route:

app.route('/users/email/:emailAddr').get(users.me);
app.param('emailAddr', users.userByEmail);

Then I called it with a $htttp.get:

$http.get('/users/email/' + newEmail).
    success(function(data, status, headers, config) {
        // do something useful with data
    }).
    error(function(data, status, headers, config) {
      $scope.error = 'Problem finding a user with that email';
    });

The problem stemmed from my inability to use one of the existing CRUD calls to send a query json object.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use findOne().

User.findOne({email: req.body.email}, function(err, user){
   //...
   callback(err);
}); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.