【发布时间】:2017-12-29 03:02:45
【问题描述】:
我有 4 个输入和按钮,它们从它们获取所有数据并通过 axios.post() 请求发送到我的 PostreSQL 数据库。不清楚 .then() 是如何工作的。所以,这是我的按钮代码,它只是调用 this.addNewPainting 函数:
<button onClick={ this.addNewPainting }>Submit</button>
这是我的 addNewPainting 函数:
addNewPainting() {
axios.post(`http://localhost:333/api/add`, {
title: this.state.titleInput,
year: this.state.yearInput,
size: this.state.yearInput,
location: this.state.locationInput
})
.then(function(response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}
在做这个项目之前,我曾经用this.setState将response.data放到数组中,但是现在我有了数据库,我只是卡住了。
这是我的控制器功能:
add_painting: (req, res, next) => {
const db = req.app.get('db');
const { title, year, size, location } = req.body;
console.log(title, year, size, location);
db.add_painting([ title, year, size, location ])
.then( () => res.status(200).send() )
.then( () => res.status(500).send() );
}
还有端点:
app.post('/api/add', paintings_controller.add_painting);
【问题讨论】:
-
我很难理解你的问题。你有什么错误吗?
-
DerekHopper 是的,这就是错误:Error: Network Error at createError (createError.js:16) at XMLHttpRequest.handleError (xhr.js:87)
标签: javascript sql node.js postgresql axios