可以用sails框架从控制器调用模型中的函数?
我有从sails命令生成的用户模型和用户控制器。 我只想调用我在控制器上创build的函数,如:
//用户模型
module.exports = { attributes: { name: { type: 'string', required: true } }, CallUserFunction: function(){ //some code goes here. } }
// userController
module.exports = { create: function(req, res){ User.CallUserFunction();//can i call function in user Model like this? } }
你想要做的不是一个实例方法。 而不是调用只是User.CallUserFunction()
你需要做这样的事情:
用户模型
module.exports = { attributes: { name: { type: 'string', required: true } }, CallUserFunction: function(){ //some code goes here. } }
用户控制器
module.exports = { create: function(req, res){ sails.models.user.CallUserFunction();//can i call function in user Model like this? } }
编辑:原谅任何错字的,因为我不是在我的开发工作站前面testing这个,我知道它非常接近sails.models
语法。