[英]How to use .create() function in sails.js?
I've read the documentation of sails.js but still I have no idea where should I use and write .create()
, .find()
etc methods. 我已经阅读了sails.js的文档,但仍然不知道应该在哪里使用和编写
.create()
.find()
等方法。 I know how to define routes in routes.js and how to define controllers. 我知道如何在route.js中定义路由以及如何定义控制器。 I have created the database connection either.
我已经创建了数据库连接。
I know how to use restful and shortcut routes to insert/fetch data from database. 我知道如何使用静态和快捷方式路由从数据库中插入/获取数据。 But I didn't find anything related to using
.create()
like methods. 但是我没有找到与使用
.create()
类的方法相关的任何内容。 I found some but they were using Angular.js and Ajax. 我找到了一些,但是他们正在使用Angular.js和Ajax。 I didn't understand a thing at all.
我什么都不懂。
Is there any good tutorial where I can learn using models and their methods(how, why and where to write them) without using Angular.js? 有没有什么好的教程可以在不使用Angular.js的情况下学习使用模型及其方法(如何,为什么以及在哪里编写)?
In Sails.js, 在Sails.js中,
Firstly we have to create api, So for example if we need method create for a user. 首先,我们必须创建api,因此,例如,如果需要为用户创建方法。 Then we generate api user by typing this command in command prompt
然后我们通过在命令提示符下键入此命令来生成api 用户
sails generate api user
Then User.js file is generated in api/models folder and UserController.js is generated in api/controllers folder. 然后在api / models文件夹中生成User.js文件,在api / controllers文件夹中生成UserController.js 。
Then we have add schema to User.js file 然后我们将架构添加到User.js文件
In User.js 在User.js中
module.exports = {
attributes : {
lastname : {
type : 'string'
},
firstname : {
type : 'string'
}
}
}
We have to write create, find, update and delete methods in UserController.js file. 我们必须在UserController.js文件中编写创建,查找,更新和删除方法。 For now will add only create method.
现在将仅添加create方法。
In UserController.js 在UserController.js中
module.exports = {
create: function(req, res, next, callback) {
var params = req.body;
//create a user
User.create(params, function(err, createdData) {
if(err){
return res.badRequest({
error: err
});
} else {
return res.json({
data : createdData
});
}
});
}
}
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.