babbaggeii
babbaggeii

Reputation: 7747

Unable to retrieve records from database

I'm trying to retrieve a record with this:

var users = User.find({ username: "andy" }).then(function(users){
    return users;
});

console.log(users);
return res.send(users);

But I get returned:

{
    "isFulfilled": false,
    "isRejected": false
}

Upvotes: 1

Views: 139

Answers (2)

VladNeacsu
VladNeacsu

Reputation: 1288

Try writing your code like this, based on the waterline examples:

Users.find({username:'andy'}).exec(function(err, result) {
  if (err) {
    return res.send(500, {error: err});
  }

  return res.json(result);
});

Upvotes: 2

Dário
Dário

Reputation: 2082

The console.log() is outside the then() statement hence it's being executed before User.find() completes and all you get is an unfulfilled promise (not the query results). Try:

var users = User.find({ username: "andy" }).then(function(users){
  console.log(users);
  res.send(users);
  return users;
});

Upvotes: 1

Related Questions