Hello Sivi,

Your problem was likely not related to the unshift method at all. That method 
is synchronous, there is no race. Something else must have caused the issue: 
front-end bug, missing content type header, the database was down or your API 
server crashed, cross domain request, but not this. In that respect, there's no 
difference between the two methods.

On the other hand, I find the second approach better, because while 
functionally equivalent, it's easier to understand what's going on at a glance.

There are a few things you could do to improve it, though.

For one, that error handler at the end. You should really add a different 
status code. And for typical clients, you would also want to send this as JSON 
payload, not raw string - it would be easier to handle the error. Furthermore, 
this way you might be exposing unneeded details about your server to the 
clients, while at the same time, _,not_ providing useful info. E.g if I was 
building a client that used this API, I wouldn't know if the error is mine 
(e.g. Missing field or some validation), yours (e.g. Server had disk full and 
can't work any more), or database error (e.g. unique constraint on an index was 
violated).
Another point would be that you're making an unneeded call to the database 
(this is MongoDB, right?). You could do the whole transaction in one go, find 
and update with a single query (since you don't seem to be checking for e.g. 
Duplicated entries anyway).
The third thing would be that you could create a light service layer and take 
all these database shenanigans out of your endpoint route. The router would get 
the id parameter from the url and the title and content from the body, and pass 
it on to a service that hid all this stuff. It adds the overhead of an extra 
file, but brings in a much easier to maintain code, reusability etc.


Hope that helps.
Zlatko

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
To post to this group, send email to nodejs@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/300f1cda-e966-4ca5-8e92-e1fe95cd1971%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to