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
 |  Show 4 more ments

1 Answer 1

Reset to default 9

So, 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