Express JS Router: persisting data
I have some "router" modules being exported into my app.js from a people.js & auth.js file.
Both of these router modules (people.js & auth.js) are require()'ing an array of objects called people, which is being module.export'd out of data.js.
Each object inside this people array represents a person', each holding an 'id' and a 'name' property.
Inside of my people.js router, one of its routes... the router.put().. allows me to change the "name" property of any of the people objects, and if successful returns a json with the new name (pic2).
Separately, inside the other auth.js router, which also require()'s that same people array, I have a single route (router.post()), which tests the req.body "name" property against all of the "people" object name properties, looking for a match, and then res.send()'s a simple 'welcome name' if a match is found... (comment pic)
I'm testing these routes inside Postman.
When I run the router.put() route from people.js, changing the name ppty in one of the 'people' objects, I'll immediately afterward run another router.get() request from people.js, which returns the people array of people objects. (pic 3), and I'll see the name-change from the router.post() request reflect in the people array. The changed name is seen in the list of objects.
However, if I then run that auth.js router.post() where it tests people for a name match and sends back a 'welcome name', it does not recognize the changed name, and the .status(401) is returned.
So it's almost like, so long as I continue to make requests from the same router (whether people.js, or auth.js), then any changes to the object in the require()'d people array will 'persist' from one request to another..
But if I make a change to that array in one router (say with a people.js route), that change to people won't persist for a auth.js route
11 Replies
The original set of names
run PUT people.js route, to change 2nd 'people' object 'name' to 'zebra'
again, run GET people.js route to see new name reflected in array
NOW, run the /login POST route from auth.js, which also require()s same people array (the one we supposedly just changed)
And try to 'login' with that new "zebra" name..
Says that name isn't recognized
so many screenshots 🧐
last file is the issue
you are creating that names array when the module is created and after that there is no update
all those names are the initial ones
keep in mind the only code in that file that is running multiple times is the function within
router.post()
everything outside of it is run exactly ONCE
run/declared
so just move that map inside your post functionahh okay
i figured it had something to do with the value being set at declaration
so now when it runs that route, it'll be assigning the most recent version of the 'people' 'name' properties
yup
only in this situation tho, because arrays are passed as references
if you were exporting a string for example and then changed it...other modules would not see that change
only arrays and objects are passed as reference export anything else and you won't see the changes because it's a copy
ok i'll have to think about that for a minute
if I was exporting a string.. meaning like if my route changed a string value instead of changing an object property value..
Then any reference to that same string from inside of the other router.. would not reflect that value change
yes