简体   繁体   English

在数据库中创建新记录(Sails.js)

[英]Creating new record in database (Sails.js)

I created model Pattern.js, looks this way: 我创建了模型Pattern.js,看起来像这样:

var Pattern = {

  adapter: 'land',

  attributes: {
    name: 'STRING',
    code: 'STRING'
  }
};

module.exports = Pattern;

And now I have two problems: 现在我有两个问题:

  1. I can't get access from my HomeController to this model. 我无法从我的HomeController访问此模型。 I tried to require it, but code fails. 我试图要求它,但代码失败。 How to get access to the models from other files? 如何从其他文件访问模型?
  2. There is no method 'create' in the documentation, so what is the way to create new record using a defined model? 文档中没有“创建”方法,那么使用已定义模型创建新记录的方法是什么?

Accessing models 访问模型

You do not have to require the model yourself, Sails does this for you. 你不必自己要求模型,Sails会为你做这件事。

Every model you define is available in the global scope as Filename (uppercase first letter) and in sails.models.filename (all lowercase) by default after the orm hook has run. 默认情况下,在运行orm挂钩后,您定义的每个模型在全局范围内都可用作Filename (大写第一个字母)和sails.models.filename (全部小写)。

sails is available in the global scope too. sails也可在全球范围内使用。

Documentation 文档

I do not know wich docs you refer to, but there is a create method available on Sails / Waterline models . 我不知道你提到的文档,但是Sails / Waterline模型上有一个create方法 In your example: 在你的例子中:

Pattern.create(data).exec(callback);

You can look up on Sails / Waterline models here: 你可以在这里查看Sails / Waterline模型:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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