Express route variables conflicts

17 views
Skip to first unread message

Eric Pan

unread,
Oct 9, 2016, 1:56:27 AM10/9/16
to nodejs
Here is my problems and I am wondering how it could happen.

Route.get("/", function(req,res) {
var id;

async.serial([f1,f2],....

f1, f2 does database insert
In f1, set the id returned by the sql
In f2, insert record with the id which is set from f1
The app works good but the problem happens when getting heavy traffic
The id in f2 is wrong, seems like from another user's request
That means the id in f1 and f2 doesn't match.
Can somebody explain to me why?
Many thanks

Ryan Graham

unread,
Oct 9, 2016, 3:14:15 PM10/9/16
to nodejs

Your little code snippet shows that you're declaring id inside the route handler to correctly scope it by request, and your async.serial() call is correct as far as is shown...  For the closure around id to work as you desire, you also need to define f1 and f2 inside the request handler. Your snippet doesn't show that much, so it's possible you are already doing that. But if you aren't doing that, and have defined those functions somewhere else, then they will be accessing a global variable shared by all requests instead.

Could you share a little more of your code?

~Ryan


--
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+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/1e7c4e19-5aca-4b0f-9a83-9439a9171170%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--

~Ryan

Reply all
Reply to author
Forward
0 new messages