pedroslopez / whatsapp-web.js

A WhatsApp client library for NodeJS that connects through the WhatsApp Web browser app
https://wwebjs.dev
Apache License 2.0
15.53k stars 3.7k forks source link

fix: TypeError in Group Invite Handling Due to in Operator on Strings #3257

Closed matheuskuster closed 2 months ago

matheuskuster commented 2 months ago

Description

This PR addresses an issue that occurs when the group.addParticipants method is called with autoSendInviteV4 set to true. Specifically, the error "Page error: Error: TypeError: Cannot use 'in' operator to search for '_serialized' in {number}@c.us" arises when calling this method. The root cause is that the code attempts to use the in operator on a string, which is not allowed in JavaScript.

Usage code example

const result = await group.addParticipants(participantIds, {
    autoSendInviteV4: true,
    comment: inviteMessage
});

The code above led to the following error at the pupPage 'pageerror' error handler:

Page error: Error: TypeError: Cannot use 'in' operator to search for '_serialized' in {number}@c.us

Solution

To resolve this issue, the code was modified to include a type check before using the in operator. This ensures that the operator is only applied to objects, preventing the error when participantIds are strings.

Related Issue

N/A

Motivation and Context

This change is required to prevent the TypeError that occurs when the in operator is used on strings. The error disrupts the flow of adding participants to a group, especially when autoSendInviteV4 is enabled. By fixing this bug, the library will handle both string and object types for participant IDs more robustly.

How Has This Been Tested

The changes were tested by simulating the scenario where participantIds includes strings formatted as {number}@c.us. The updated logic successfully prevents the TypeError and allows participants to be added without issue. The test environment included both cases: participantIds as strings and as objects containing the _serialized property.

Types of changes

Checklist