我正在尝试使用 Sails 在我的数据库中创建一个新客户。当我通过 URL 手动输入参数时,这就像您期望的那样工作,但是当我通过表单使用 POST 方法时,我收到 404 not found 错误,当我将页面刷新到相同的 URL 时,这没有什么意义,我收到一个新错误,通知我未定义的属性(这是有道理的,因为我没有定义任何属性)。我检查了 Chrome 控制台,POST 方法似乎以正确的 URL 格式发送数据。
客户控制员
/**
* CustomerController
*
* @description :: Server-side actions for handling incoming requests.
* @help :: See https://sailsjs.com/docs/concepts/actions
*/
module.exports = {
'new': function (req, res) {
res.view();
}
};
new.ejs(新客户创建 View )
<form action="/customer/create" method="POST">
<h2> Create customer</h2>
<input type="text" placeholder="Name" name="name" ></br/>
<input type="text" placeholder="Email" name= "email" ></br/>
<input type="text" placeholder="State" name = "state" ><br/>
<input type="submit" value="Create Customer"/>
</form>
Customer.js(模型)
/**
* Customer.js
*
* @description :: A model definition. Represents a database table/collection/etc.
* @docs :: https://sailsjs.com/docs/concepts/models-and-orm/models
*/
module.exports = {
attributes: {
name: {
type: 'string',
required: true
},
email: {
type:'string',
isEmail: true
},
state: {
type: 'string'
}
},
};
最佳答案
已修复! **
在旧版本的 sails 中,如果您想使用“创建”方法,您必须明确地将其指向 URL 并包含“/create”。在当前版本中,它似乎不需要这个并且直接发布到“/customer”(或您使用的任何“模型”)会自动创建一个新记录。
关于javascript - 风 sails JS。无法从 POST 表单返回 'create',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51414105/