【发布时间】:2018-01-09 01:31:34
【问题描述】:
我正在尝试从 Bootstrap 表单元素中检索数据,并使用 Express 和 Knex 将其保存到 PostgresSQL 数据库。运行路线时没有错误;但是,表单中的数据保存为空。这是我的表单元素(我正在使用 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
}
任何帮助将不胜感激!
【问题讨论】:
标签: javascript reactjs express knex.js