我试图弄清楚如何将Sails.js与node-machine-syntax一起使用。这是我的航班列表行动,简单,没什么疯狂的。我在尝试这个:
/**
* FlightsController
*
* @description :: Server-side actions for handling incoming requests.
* @help :: See https://sailsjs.com/docs/concepts/actions
*/
module.exports = {
friendlyName: 'flights list',
description: 'list all flights',
exits: {
success: {
responseType: 'view',
viewTemplatePath: 'pages/list'
},
notFound: {
description: 'No flight was found in the database.',
responseType: 'notFound'
}
},
fn: async function (exits) {
var flights = await Flights.find({});
// If no flight was found, respond "notFound" (like calling `res.notFound()`)
if (!flights) { return exits.notFound(); }
// Display the hompage view.
return exits.success({flights});
}
};
但是,当我尝试访问页面/列表时,它向我显示500服务器错误页面,其中包含以下消息:“TypeError:exits.success不是函数”。我真的不明白它,因为那是你希望你使用的语法..它缺少“输入”对象,但我删除它因为我真的不需要它,只想输出我的数据库中的航班..而且它似乎没有无论如何要解决问题。
有任何想法吗?谢谢!
我的飞行模型是这样的:
module.exports = {
attributes: {
company:{
type: 'string',
required: true
},
takeoff:{
type: 'string',
required: false
}
},
};
我的页面list.ejs字面上显示“LIST”这个词,就像那样。
您还需要放置inputs
对象,如下所示:
/**
* FlightsController
*
* @description :: Server-side actions for handling incoming requests.
* @help :: See https://sailsjs.com/docs/concepts/actions
*/
module.exports = {
friendlyName: 'flights list',
description: 'list all flights',
inputs: {},
exits: {
success: {
responseType: 'view',
viewTemplatePath: 'pages/list'
},
notFound: {
description: 'No flight was found in the database.',
responseType: 'notFound'
}
},
fn: async function (inputs, exits) {
var flights = await Flights.find({});
// If no flight was found, respond "notFound" (like calling `res.notFound()`)
if (!flights) { return exits.notFound(); }
// Display the hompage view.
return exits.success({flights});
}
};