1

I am trying to run a simple query in my local Postgres db to return the User with an ID of 1 and save that value in the variable record. I'm using Sails.js and am in the Sails Console when I'm running these commands. I can get the query to print the object by running

var record = User.find(1).exec(console.log);,

but I can't seem to figure out how to save that value in a variable.

I've tried quite a few other things. await doesn't work in the Sails Console and all their documentation seems to use that.

I've also tried:

var record = User.find(1).exec(function(err, u){
    return u[0];
});

var record = User.find(1).exec(function(err, u){
    return u;
});

If I run:

var record = User.find(1).exec(function(err, u){
    console.log(Object.keys(u[0]));
});

it does print out all the keys to the user I'm querying, so I'm able to access the object, just can't figure out a way to save that object in a variable inside the Sails Console.

Any ideas?

Joseph Gill
  • 1,067
  • 11
  • 18
  • I am not sure I understand your Q. Are you trying to change the value of an attribute in Sails console? If so it should look like this: `User.updateOne(1).set({ firstName: Peter}).log()` – Raqem May 23 '19 at 14:52
  • Honestly I'm just trying to query the database and save the returned records in a variable. It seems like something I should be able to accomplish. In the rails console, I would just do `record = User.find(1)` and then I could use record as a user object. – Joseph Gill May 23 '19 at 14:55
  • 1
    First declare variable `let record;` and then `User.find(1).exec(function(err, u){ record = u; });` This also works `User.find(1).then(function(u){ record = u; });` – Dejan Kubaša May 24 '19 at 12:55

0 Answers0