D
D
da_da_ya_neudachnik2022-03-16 08:03:51
JavaScript
da_da_ya_neudachnik, 2022-03-16 08:03:51

Event vk api not working?

There is a code, if the name of the conversation changes, then a check is performed: whether the user is an administrator or not. And it gives me an error: " Handle polling update error: TypeError: Cannot read properties of undefined (reading 'role') "
Code:

vk.updates.on(['chat_title_update'], (next, context) => {
  const peer = chats.filter(x => x.id === next.chatId)[0];
  const user = peer.members.filter(x => x.id === next.eventMemberId)[0];
        if(user.role==0){
           vk.api.messages.removeChatUser({ chat_id: next.chatId, user_id: user.id });
           vk.api.messages.editChat({ chat_id: next.chatId, title: peer.name });
        }
        return context();
});

JSON excerpt:
[
{
"id": 1,
"cid": 0,
"name": "test",
"owner": 0,
"greeting": "Not set",
"members": [
{
"id" : 0,
"ban": false,
"points": 0,
"warn": 0,
"mute": 0,
"role": 4
},
{
"id": 1,
"ban": false,
"points" : 0,
"warn": 0,
"mute": 0,
"role":0
}
]
}
]

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
da_da_ya_neudachnik, 2022-03-16
@da_da_ya_neudachnik

The problem was that this event does not send 'eventMemberId', it sends 'senderId'.

D
defriz, 2022-03-16
@defriz

print the next object to the console and see what the exact parameters are, there may also be an error due to the fact that the user is simply not found in the database and returns undefined

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question