I have two arrays of objects that I am exporting at the end of server.js
with module.exports
which will be used in another file to display scores, scores.js
. My problem occurs when I try and reset these arrays when a new games begins and for some reason some data seems to not update in scores.js
whilst others do.
I have been researching and think it has something to do with the way javascript saves objects and I have been trying different ways of deep/shallow cloning however nothing seems to work.
More specifically, there is a property in one of the arrays of objects called playersChoosing
which is also an array and is the one that seems not to change even if I completely set the array of objects to an empty one in the server.js
file.
Note: This is my first time properly using node.js so could definitely be missing something very basic. Any help is appreciated!
These are examples of the two objects:
Current player definitions [
{ definition: 'maiming; mutilation', playersChoosing: [ 'player4' ] },
{
definition: 'blah blah',
id: 'vIe_bxsTjg4OzFdJAAAB',
playersChoosing: [],
choseCorrect: false
},
{
definition: 'whatever',
id: 'b7lJloolPhLGdrrnAAAD',
playersChoosing: [ 'player1', 'player2', 'player3' ],
choseCorrect: true
},
{
definition: 'foo',
id: 'o7rTbhVp8WllpLTwAAAF',
playersChoosing: [],
choseCorrect: false
},
{
definition: 'bar',
id: 'lMnqOsIzdfQMBWPVAAAH',
playersChoosing: [],
choseCorrect: false
}
]
Game connections [
{
id: 'vIe_bxsTjg4OzFdJAAAB',
nickname: 'player1',
ready: true,
score: 2
},
{
id: 'b7lJloolPhLGdrrnAAAD',
nickname: 'player2',
ready: true,
score: 4
},
{
id: 'o7rTbhVp8WllpLTwAAAF',
nickname: 'player3',
ready: true,
score: 1
},
{
id: 'lMnqOsIzdfQMBWPVAAAH',
nickname: 'player4',
ready: true,
score: 1
}
]
CodePudding user response:
A contrived example of exporting functions that manage data housed in the singleton module, rather than the data itself.
let definitions = [
{ 1: 'a' },
{ 2: 'b' },
{ 3: 'c' },
]
export function clearDefinitions(){
definitions.length = 0
return true
}
export function currentDefinitions(){
return definitions // internal array
return definitions.slice() // or copy of internal array
}
export function addDefinition(obj){
const cloned_obj = JSON.parse(JSON.stringify(obj)) // copy of arg
definitions.push(cloned_obj)
return true
}
Then there are data structures like a Map
which already do all the helpers for you. (.clear
.get
.set
.entries
) that will act upon itself
export const definitions = new Map()
Which can be extended
export class NedMap extends Map {
gameTransform(obj) {
// do things
this.set(obj.id, obj)
return this
}
}
export const definitions = new NedMap()
Depending on how much you want to protect the modules data, something like Immutable might be handy to cover more of the edge cases of handling shared data in JS and the unexpected modifications that can come from that sharing.
For example, in the initial array code, Even though a copy of the definitions
array is returned from slice
the objects inside the array still reference the same original objects.
Something outside the module setting definitions.1 = 'argh'
still modifies the shared object due to that reference to the original modules object. That can be useful in cases, or a pain in others.
CodePudding user response:
let's take a look at this demo.I write three .js file something like:
// values.js
module.exports = {
name: 'jack',
score: [34]
}
// container.js
const values = require('./values')
values.score = []
module.exports = {
name: 'peter',
container: values
}
// main.js
const values = require('./values')
const p = require('./container')
console.log('values: ', values)
console.log('p: ', p.container)
Ok, you run node main.js
;
you find that values and p is same.
module.exports
just export an object, and in main.js and container.js, we share the reference of that object values
.You change values
in container.js, and the changes will be seen in main.js values
.
Maybe you're failed to share the reference, and then you cannot find the changes.