S
S
Sergey2018-11-26 13:54:44
JavaScript
Sergey, 2018-11-26 13:54:44

Why, when assigning the value of variable "1" to variable "2", variable "2" is replaced by "1"?

Good day, in general, I am self-taught, I am making a telegram bot on Node.JS.
I am implementing the possibility of placing an order, but in the process there was a problem that I do not understand how to solve.
You need to assign the value of the variable "1" to the variable "2". As a result, it turns out that nothing changes, and the value of the variable "2" changes to the variable "1", that is, everything happens the other way around.
I don't know how to explain it clearly, so here's how I did it:
There is an object with "sessions", when the user sends a message, the following happens:

session[ID] = session[ID] || {
    log: [PathTmpl.main_menu],
    inline_log: '',
    name: ctx.from.first_name,
    last_name: ctx.from.last_name,
    auth: 'new',
    admin: false,
    orders: [],
    phone: '',
    delivery: emptyAdress,
    order: emptyOrder
  }

Pay attention to the order and orders variables .
The emptyOrder object is a constant, with keys and empty values
const emptyOrder = {
  id: 0,
  name: '',
  pack: '',
  code: '',
  amount: '',
  price: ''
}

Orders is an array, the selected products from the order object are added to it .
As the product is selected, data is added to the order :
session[ID].order.code = code
…
session[ID].order.name = infoTxt.name
…
session[ID].order.pack = infoTxt.packing
…
session[ID].order.price = infoTxt.cost
…
session[ID].order.amount= amountNum

After that, order is pushed into the orders array and cleared, preserving the keys:
session[id].orders.push(session[id].order)
session[id].order = emptyOrder

Everything seems to be logical, but it’s worth looking at what is in the variables IMMEDIATELY after these lines
I output it to the console
console.log('\n session[id].orders : \n', session[id].orders) // массив
console.log('\n session[id].order : \n', session[id].order) // объект
console.log('\n emptyOrder : \n', emptyOrder) // константа

5bfbcfb518a5c896625501.png
You can already notice that the emptyOrder variable has been replaced with the value order , but this shouldn't happen. And the order itself hasn't been cleared...
And here's what happens if you add another product:
5bfbd032cc586958747731.png
That is, it does not add a new product to the array, but replaces all elements with the last selected product.
5bfbd08173c77765364782.png
I don't understand what it is and how to solve it.
The same is true for shipping information. I noticed that after one user has entered the delivery address, it is added to the emptyAdress constant , and after that the next user is shown information from the same emptyAdress , that is, he sees the data of another user!
Curious if anyone else has experienced this and what are the possible solutions?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dollar, 2018-11-26
@Sergey-Nag

You are not assigning a value, but a reference to a value. This is the case with objects and arrays.

let a = {x:1};
let b = a; // Теперь a и b указывают на один и тот же участок памяти.
b.x = 2;
console.log(a.x); // 2    Ах, вот оно как!

//Как скопировать:
let c = {}; //Создается новый объект и под него выделяется новая память
c.x = a.x; //Копирование значений, т.к. x это не объект.

//Универсальный вариант (копируются все свойства):
a.y = 11;
a.z = 111;
let d = Object.assign({}, a); 
console.log(d); // {x:1, y:11, z:111}

Object.assign()

P
Pretor DH, 2018-11-26
@PretorDH

When you assign an object, you are assigning a reference to the object, not making a copy of it. And then you overwrite the fields of the emptyOrder itself, through a link to emptyOrder in session[ID].order. Need to make a copy of the data...
session[ID] = session[ID] || {
log: [PathTmpl.main_menu],
inline_log: '',
name: ctx.from.first_name,
last_name: ctx.from.last_name,
auth: 'new',
admin: false,
orders: [],
phone: '',
delivery: emptyAdress,
order: Object.assign({}, emptyOrder)
}
PS Don't forget to include a polyfill for Object.assign or use a similar framework method.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question