Answer the question
In order to leave comments, you need to log in
How to push an object into an array?
Wrote such a piece of code, but something is not right.
let users = [];
...
users[socket] = {}; // Создаем новый объект
users[socket].id = results[0].id; // ID пользователя из бд
users[socket].nickname = results[0].nickname;
users.push(users[socket]); // Пушим, надо сохранить сокет как ключ, ибо по нему идёт перебор пользователей
...
Answer the question
In order to leave comments, you need to log in
const users = [];
// ...
const server = net.createServer((socket) => {
const newUser = {};
newUser.socket = socket;
newUser.id = results[0].id; // ID пользователя из бд -- не знаю уж где вы его возьмете
newUser.nickname = results[0].nickname;
users.push(newUser);
c.on('data', function(data) {
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question