我试图从Bootstrap表单元素中检索数据,并使用Express和Knex将其保存到PostgresSQL数据库.我运行路线时没有错误;但是,表单中的数据保存为null.这是我的表单元素(我正在使用React):
render() {
return (
<form>
<div className ="form-group">
<label>Add a Note:</label>
<textarea className="form-control" name="note" rows="5">
</textarea>
</div>
<button onClick={this.handleClick} className="btn btn-primary"
type="submit">Submit</button>
</form>
)
}
这是我对POST路线的提取:
handleClick(e) {
e.preventDefault()
fetch('/create-note', {
method: 'POST'
})
}
这是我的Express POST路线(app.use(bodyParser.json())包含在此文件中):
app.post('/create-note', (req, res) => {
postNote(req.body.note)
.then(() => {
res.sendStatus(201)
})
})
这是Knex postNote函数:
export function postNote(newNote) {
const query = knex
.insert({ note_content: newNote })
.into('notes')
return query
}
任何帮助,将不胜感激!
解决方法:
使用POST请求,您可能必须等待数据正文准备就绪.试试这个
app.post('/create-note', (req, res) => {
var body = '';
request.on('data',function(data) { body += data; });
request.on('end', function(data) {
postNote(body)
.then(() => {
res.sendStatus(201)
})
});
})
原文地址:https://codeday.me/bug/20190701/1349387.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。