I bumped into some odd problem in my node application. If i take contents of one variable and assign to another it somehow crates a link between them in the way that if i change content in second variable it is also affecting the first one that i took content from.
this is my server structure:
var body = {};
now lets populate body:
body[_session] = {
content: { /* some data here */ },
sockets: []
};
then at some point i do this:
body[new_session] = {
content: body[_session].content,
sockets: []
};
at that point, changes made to new_session content will be also made on original _session content.
i solved this problem by doing this:
body[new_session] = {
content: JSON.parse(JSON.stringify(body[_session].content)),
sockets: []
};
but it seem like costly operation and something that could be avoided i just dont know yet how, maybe someone have an idea, why its happening and how it can be solved without much effort.