S
S
Skrolea2016-01-29 12:22:49
JavaScript
Skrolea, 2016-01-29 12:22:49

What's wrong with saving?

Good afternoon.
I save to the database (in mongo)

function addProducts(name) {
            var data = {
                name: name
            };
            return $http.post('/products/add', data)
                    .then(addProductsComplete)
                    .catch(addProductsFailed);
            function addProductsComplete(response) {
                console.log('Сохранено');                
                return response.data;
            }
            function addProductsFailed(error) {
                console.log('error in POST Products service ');
            }
        }

In the server route, respectively
router.post('/add', function (req, res) {
    var product = new Products({
        name: req.body.name
    });
    product.save(function (err) {
        if (err)
            throw err;
    });

});

And everything is fine - the database is saved after the fact. But after a while, an error appears in the browser console
error in POST Products service
i.e. still getting an error. Delivered console.log('Сохранено');but it is not caused.
How to catch an error or what is wrong? Taking into account the fact that the database is still saved as it should.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
pomeo, 2016-01-29
@Skrolea

So you don’t send a response, add res.sendStatus(200)
Otherwise, it gives you an error by timeout

L
lega, 2016-01-29
@lega

Print the error to the console.

post('/products/add' respectively post('/add'
Why is it appropriate? Urls are different.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question