admin管理员组文章数量:1323737
I want to post a new document to a database using Axios to React. Thankfully I can do it, but I also want log a message to the console that says "New post has been inserted".
Here is my front end code:
newTodo(todo){
axios.post('/todo', {
title: todo.title,
description: todo.description
})
.then(function (response) {
console.log('New post has been inserted!');
})
.catch(function (error) {
console.log(error);
});
}
My Back End (Nodejs)
server.post('/todo', (req, res) => {
// Inserting new todo
var newTodo = new todo({
title: req.body.title,
description: req.body.description,
key: '',
date: moment().format('lll')
});
newTodo.save((e, savedTodo) => {
if(e){
console.log(e);
} else {
todo.findById(savedTodo._id, (e, foundTodo) => {
if(e) {
console.log(e);
} else {
foundTodo.key = foundTodo._id.toString();
foundTodo.save((e, updatedTodo) => {
if(e){
console.log(e);
} else {
console.log(updatedTodo);
}
});
}
})
}
})
});
The request is successful but the message is never logged. Additionally, there are no errors logged. Please tell me what I am doing wrong or if there is another way to do this.
I want to post a new document to a database using Axios to React. Thankfully I can do it, but I also want log a message to the console that says "New post has been inserted".
Here is my front end code:
newTodo(todo){
axios.post('/todo', {
title: todo.title,
description: todo.description
})
.then(function (response) {
console.log('New post has been inserted!');
})
.catch(function (error) {
console.log(error);
});
}
My Back End (Nodejs)
server.post('/todo', (req, res) => {
// Inserting new todo
var newTodo = new todo({
title: req.body.title,
description: req.body.description,
key: '',
date: moment().format('lll')
});
newTodo.save((e, savedTodo) => {
if(e){
console.log(e);
} else {
todo.findById(savedTodo._id, (e, foundTodo) => {
if(e) {
console.log(e);
} else {
foundTodo.key = foundTodo._id.toString();
foundTodo.save((e, updatedTodo) => {
if(e){
console.log(e);
} else {
console.log(updatedTodo);
}
});
}
})
}
})
});
The request is successful but the message is never logged. Additionally, there are no errors logged. Please tell me what I am doing wrong or if there is another way to do this.
Share Improve this question edited Oct 15, 2019 at 5:36 Yusuf asked Feb 5, 2018 at 18:36 YusufYusuf 2,3881 gold badge20 silver badges26 bronze badges 9- 1 Check the console for errors and try setting a break point in your code if possible. – Phiter Commented Feb 5, 2018 at 18:42
- 2 Open network tab in developer tools and check the api response what is returned ? – Amr Labib Commented Feb 5, 2018 at 18:42
- are you using any middle ware? – JPRLCol Commented Feb 5, 2018 at 18:46
- After, 1 or 2 minutes there are two errors POST localhost:3000/todo net::ERR_CONNECTION_RESET Uncaught (in promise) Error: Network Error – Yusuf Commented Feb 5, 2018 at 18:47
- if you are using a nodejs server for /todo check the server, and check if POST is 200 OK and not pending in the NETWORK tab. – Arber Sylejmani Commented Feb 5, 2018 at 19:30
1 Answer
Reset to default 9So, whenever you need to return a result, for example if you need to return: updatedTodo you need to use:
res.send(updatedTodo)
本文标签: javascriptAxios not handling responseStack Overflow
版权声明:本文标题:javascript - Axios not handling response - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742119194a2421615.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论