[英]CRUD in Sails.js model
I have created a Sails.js Model by using command "sails generate model". 我已经使用命令“ sails generate model”创建了Sails.js模型。 The model is generated as follows: 该模型生成如下:
module.exports = {
connection: 'someMysqlServer',
attributes: {
amount : { type: 'float' },
country : { type: 'string' },
month : { type: 'string' }
}
};
I am trying to use .create() function of an instance of this model , inside a controller as follow , but I receive error : 我正在尝试在控制器内使用此模型实例的.create()函数,如下所示,但出现错误:
var myModel = require ('../models/Info') ;
module.exports = {
getInfo: function (req, res) {
console.log("inside getInfo controller");
myModel.create({amount:3.5 , country:'spain' , month: 'january' }) ;
} ,
}
I receive the following error while using .create() function : 使用.create()函数时收到以下错误:
error: Sending 500 ("Server Error") response:
TypeError: Object #<Object> has no method 'create'
at module.exports.getInfo (/vagrant_data/inputinterface/b2bInputInterface/api/controllers/InputPageController.js:38:26)
at routeTargetFnWrapper (/usr/lib/node_modules/sails/lib/router/bind.js:178:5)
at callbacks (/usr/lib/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
at param (/usr/lib/node_modules/sails/node_modules/express/lib/router/index.js:138:11)
at param (/usr/lib/node_modules/sails/node_modules/express/lib/router/index.js:135:11)
at pass (/usr/lib/node_modules/sails/node_modules/express/lib/router/index.js:145:5)
at nextRoute (/usr/lib/node_modules/sails/node_modules/express/lib/router/index.js:100:7)
at callbacks (/usr/lib/node_modules/sails/node_modules/express/lib/router/index.js:167:11)
at /usr/lib/node_modules/sails/lib/router/bind.js:186:7
at alwaysAllow (/usr/lib/node_modules/sails/lib/hooks/policies/index.js:209:11) [TypeError: Object #<Object> has no method 'create']
According to the following http://sailsjs.org/#/documentation/reference/waterline/models/create.html this method should be accessible automatically ? 根据以下http://sailsjs.org/#/documentation/reference/waterline/models/create.html,此方法应该可以自动访问吗?
Models are globalized for you by Sails; Sails为您全球化了模型。 there is no need to attempt to require()
them on your own nor will this work as intended. 无需尝试自行使用require()
,也require()
按预期进行。
If your model file lives in api/models/Info.js , then your controller would look something like: 如果您的模型文件位于api / models / Info.js中 ,那么您的控制器将类似于:
module.exports = {
getInfo: function (req, res) {
Info.create({amount:3.5 , country:'spain' , month: 'january' });
}
}
Rather than trying to follow the Waterline docs when creating a Sails app, you'll have a lot more luck following the Sails docs for models . 在创建Sails应用程序时,与其尝试遵循Waterline文档,还不如跟随Sails文档中的Models运气。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.