Zdravo Zlatko, 

Thank you very much for your reply, that is very helpful, I really needed 
to know if one method was doing something better than the other (I 
suspected they were doing the same thing in a different way).

I will try and amalgamate the mongodb queries into one (to 'findAndUpdate' 
etc), I just have a big worry that there is a 'hole' in my code somewhere 
that means that something is not being added where it should be 
(sometimes), and I want to make sure that it is, and that if it isn't that 
an error is thrown. The application is currently hosted on Heroku's free 
server and mlab's free mongodb, so I'm wondering if maybe that has 
something to do with it - I'm currently in development but I am noobish and 
have a great fear of releasing this app to production (because it works for 
me), and there being a significant problem with it. 

Hvala. 


On Thursday, January 24, 2019 at 9:19:04 AM UTC+1, Zlatko wrote:
>
> 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/615e14b7-4485-427b-b78d-bee7c93bf3a6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to