Deleting a topic in node with mongoose - node.js

I am working on a project with node, express, mongo. I am trying to delete a topic.
This is my code:
router.get('/:id/delete', function(req, res, next){
var topicId = req.params.id;
console.log('Logging topic id: ' + topicId);
Topic.findById(topicId, function(err, topic)
{
if(err)
{
console.log('There was no topic with this ID');
return next(err)
}
else
{
Topic.remove(topicId, function(err){
res.render('/mytopics');
console.log('Topic deleted successfuly');
});
}
});
});
The thing is it won't even come into the first console log where I am logging the topic id. So I am wondering if my query is built right?

In RESTful API structure the delete route should expect DELETE request. Try out following code:
router.delete('/:id/delete', function(req, res, next) {
// ...
For additional resources about this issue be sure to check out:
Recommended way to delete object in MongoDB based on a route

Related

express post 500 error without router/subsite

My setup is as follows:
posting to /register will take the arguments and register a user via passport and mongoose. If this returns an UserExistsError the server sends this info to the client (via http error handling).
However the server also displays a 500 server error which should not occur.
This is because of the next() which as far as I understand routes the client to /register. /register itself does not exists as a page (only as the postadress as stated in the code)
So my question is: How to handle the response to not be an error or supress it? Can I use something else instead of next() to stop the redirect to /register? I just want the server to stop doing anything/going out of that function at that point.
Code:
app.post('/register', function(req, res, next) {
console.log('server registering user');
User.register(new User({username: req.body.username}), req.body.password, function(err) {
let tempstring = ""+err;
if(tempstring.indexOf("UserExistsError") !== -1){
return next(err); //get out of the function and into normal operation under '/'
}
});
});
This topic is bugging me and I might just missunderstand something trivial.
Even if /register is a post only route you still need to send a response. If you don't send a response of some kind, the request will hang and eventually timeout in the browser. I would suggest sending a json response like so.
app.post('/register', function(req, res, next) {
console.log('server registering user');
User.register(new User({username: req.body.username}), req.body.password, function(err) {
let tempstring = ""+err;
if(tempstring.indexOf("UserExistsError") !== -1){
return next(err); //get out of the function and into normal operation under '/'
}
res.json({message: 'message here'});
});
});
This will send a 200 OK reponse with some json in the body.
If you just want to pass the request down the line you need to call next without an err object like so.
app.post('/register', function(req, res, next) {
console.log('server registering user');
User.register(new User({username: req.body.username}), req.body.password, function(err) {
let tempstring = ""+err;
if(tempstring.indexOf("UserExistsError") !== -1){
return next(err); //get out of the function and into normal operation under '/'
}
//call next without an error
next();
});
});
I am not sure if this is what you are trying to achieve, but if there is no route that matches it will just go to an error 500.

Express Not able to get request body value

I am trying to learn the MEAN stack by following a tutorial for CRUD operations using REST Api with express and mongoose. I am able to run the GET and DELETE operations but I am not getting the POST and PUT operations because I am not getting the required request body values.
router.post('/', function (req, res, next) {
var todoObject = {
name: req.body.name
};
Todo.create(todoObject, function (err, todo) {
if (err) res.send(err);
Todo.find(function (err, todos) {
if (err) res.send(err);
res.json(todos);
});
}); });
As you can see from the code that I am setting the name parameter of the json object using the request body value which I pass through POSTMAN. A new document is getting created but the name value is unset in the database.
However, giving the name directly works. Please help me.
the simplest and safest way is to send JSON object from the client using RAW data.
so ,to implement it follow the steps below:
Set the Content/type to application/json in the Headers
Select raw type and put your JSON object in the Body
And finally Your code should be like:
router.post('/', function (req, res, next) {
var todoObject = req.body;
Todo.create(todoObject, function (err, todo) {
if (err) res.send(err);
Todo.find(function (err, todos) {
if (err) res.send(err);
res.json(todos);
});
});
});
You need to use Content-Type: application/json in Headers if it is with JSON format.

Node.js delete request

I have a node.js application with Express for routing and Mongoose for the database access.
I have this HTTP DELETE request that is deleting an entry in the database if certain requirements are met.
.delete(function (req, res) {
Movie.findOne({_id: req.params.id
}, function (err, movie) {
if (err)
res.send(err);
for (var i = 0, len = movie.actors.length; i < len; i++) {
if (movie.actors[i].actor == "Chuck Norris"){
res.status(403).send({ message: 'Cannot delete Chuck Norris' });
}
else {
Movie.remove({_id: req.params.id
}, function (err, movie) {
if (err)
res.send(err);
res.json({message: 'Movie deleted'});
});
}
}
});
});
If I send a HTTP DELETE with the ID of a film, it will check in the actors list if Chuck Norris is in the movie and I will prevent the deletion if he is there.
The problem is that my console is returning me this error :
Error: Can't set headers after they are sent.
So I presume that this an issue with my callbacks. Due to the asynchronus nature of node.js the "slow" database call made that my .delete sent the headers before the findOne finished ?
How can I manage to validate before deletion and send a proper http error code if the deletion is not possible ?
Be careful when you respond to a request multiple times in the same scope like that. On error you should prepend return to res.send(err) so that execution will not continue further. The same goes for your res.status() in the for-loop.

Save to database on change, angular and node js

I'm converting an MS Access database to a webapp. I'm using Angular JS, Node JS with the express framework and MySQL as database.
In ms access you don't have any edit/save features. When you edit something, the database changes instantly. I like this. Feels smooth. So I want to have this the same way in the web app. My question is. Will there be any problems with this approach in my webbapp?
This is a piece of my node js code which updates the database with a restcall:
/*
Post /api/products/ HTTP/1.1
*/
exports.editProduct = function(req, res) {
console.log(req.body);
var post = [{title_en: req.body.title_en},req.params.id];
if (connection) {
connection.query("UPDATE products SET ? WHERE id = ?", post, function(err, rows, fields) {
if (err) throw err;
res.contentType('application/json');
res.write(JSON.stringify(rows));
res.end();
});
}
};
And on the client side I use the a the $resource object
$scope.save = function(){
$scope.product.$save(function(){
console.log('Save successfull);
});
};
And in the view. I simply have inputs with ng-change:
<input ng-model="product.title_en" ng-change="save()".
Will this work good in production mode with a couple hundred users? Is the chances of blocking/crashing etc?
The only thing I see is if (err) throw err;
if there is an error the server crash so change it with a json response with a 500 status.
By the way express has a build-in way to output json
It's better off to validate title_en and id
exports.editProduct = function(req, res) {
console.log(req.body);
var post = [{title_en: req.body.title_en},req.params.id];
if (connection) {
connection.query("UPDATE products SET ? WHERE id = ?", post, function(err, rows, fields) {
if (err) {
return res.json(500,{ error: 'Cannot update the product' });
}
res.json(200,rows);
});
}
an other thing try to use restangular instead of resource it's a lot of fun :)
};

Response headers of previous request affecting current request

The following code is the user-facing part of a new node app we are building:
var loadInvoice = function(req, res, next) {
Invoice.findById(req.params.invoiceId, function (err, invoice) {
if (err) {
res.send(404, 'Page not found');
} else {
req.invoice = invoice;
next();
}
});
};
app.namespace('/invoices/:invoiceId', loadInvoice, function () {
app.get('', function(req, res){
var templateVals = {
//some template data
};
res.render('paymentselection', templateVals);
});
app.post('', function(req, res){
var data = {
// some data for the apiCall
};
someAPI.someRequest(data, function(err, data) {
console.log(res.status());
res.redirect(data.url);
});
});
});
The first method returns a confirmation page where the user presses a button to post to the same url, which triggers a redirect to an external website.
This all works exactly once. Every second request will crash the app with the message Cant set headers after they are sent. After carefull inspection of the code I could find no reason for this to happen so I added the console.log line which indeed confirms the location header has been set. But it is set to the value i got from someAPI on the previous request not the current one.
This makes absolutely no sense to me. I do not store this value anywhere nor do I do caching or persistence of this data in any way.
Does anybody know what could be causing this?
I use express, express-namespace, mogoose and swig
I found out the problem was being caused bij the 'Restler' libaray used within 'someAPI'. I have no idea how this is possible but swapping it out with something else fixed the problem.

Resources