简体   繁体   中英

Load Sails programmatically and work with models

I see that I can access models at sails.models after loading sails programmatically, but I can't figure out how to find and update models. I want to do something like this, is it possible?

const Sails = require('sails').constructor;
const sails = new Sails();
await sails.load();
const user = await sails.models.user.findOne({id:1});

I get an error that findOne does not exist. No methods exists on the models from sails.models , only the customToJSON .

My syntax was wrong, this is the right way:

const Sails = require('sails').constructor;
const sails = new Sails();

sails.load({}, async () => {
  //console.log(sails.models.user);
  const u = await sails.models.user.findOne({id: 7});
  console.log(u);

  sails.lower( (err) => {
      if (err) {
        return console.log("Error occurred lowering Sails app: ", err);
      }
      console.log("Sails app lowered successfully!");
    }
  );
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM