Skip to content

fix: getContacts resolving all promisses#201698

Open
BenyFilho wants to merge 5 commits intowwebjs:mainfrom
BenyFilho:fix_getContacts
Open

fix: getContacts resolving all promisses#201698
BenyFilho wants to merge 5 commits intowwebjs:mainfrom
BenyFilho:fix_getContacts

Conversation

@BenyFilho
Copy link
Copy Markdown
Member

Fixing getContacts resolving all promises

Description

Fixing getContacts resolving all promises

Related Issue(s)

Closes #201690

Testing Summary

Test Details

client.on('ready', async () => {
    const contacts = client.getContacts();
    console.log(contacts);
});

Environment

  • Machine OS:

Windows 10

  • Phone OS:
    Android 11

  • Library Version:

1.34.6 - #main branch with recent changes

  • WhatsApp Web Version:

2.3000.1036716141

  • Browser Type and Version:

Chrome 146.0.7680.178

  • Node Version:

18.20.2

Type of Change

  • Dependency change (package changes such as removals, upgrades, or additions)
  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Non-code change (documentation, README, etc.)

Checklist

  • My code follows the style guidelines of this project.
  • All new and existing tests pass (npm test).
  • Typings (e.g. index.d.ts) have been updated if necessary.
  • Usage examples (e.g. example.js) / documentation have been updated if applicable.

Fixing getContacts revolve all promisses
@github-actions github-actions bot added api changes API modifications utility Utility code labels Apr 6, 2026
@BenyFilho BenyFilho added the waiting for testers Needs testing label Apr 6, 2026
@Adi1231234
Copy link
Copy Markdown
Contributor

@BenyFilho Nice work, thanks for fixing this!

A few suggestions:

  • createWid(contact.id) can be dropped - contact.id is already a Wid on the model (createWid(contact.id) === contact.id). Unlike getContact() where the id comes as a string from Node.
  • contact.businessProfile = bizProfile is redundant - Contact.initialize() calls addChild("businessProfile", ...) which links it to the collection. When find() updates the collection, the contact reflects it automatically. Same for new contacts via the change:isBusiness listener.
  • Promise.resolve(...) isn't needed - Promise.all handles plain values.
  • find() can reject with ServerStatusCodeError - one failed profile would take down the whole Promise.all. Worth adding a .catch().

Suggested version:

window.WWebJS.getContacts = async () => {
    const contacts = window.require('WAWebCollections').Contact.getModelsArray();
    return Promise.all(
        contacts.map(async (contact) => {
            if (contact.isBusiness || contact.isEnterprise) {
                await window.require('WAWebCollections')
                    .BusinessProfile.find(contact.id)
                    .catch(() => {});
            }
            return window.WWebJS.getContactModel(contact);
        })
    );
}

@Adi1231234
Copy link
Copy Markdown
Contributor

Great! Approved :) @BenyFilho

themazim added a commit to themazim/whatsapp-web.js that referenced this pull request Apr 8, 2026
getContacts returned an array of unresolved promises instead of
contact data because contacts.map(async ...) was not wrapped in
Promise.all(). Also simplifies BusinessProfile.find() by passing
contact.id directly instead of creating a Wid first.

Port of upstream wwebjs#201698.
@themazim
Copy link
Copy Markdown
Contributor

themazim commented Apr 8, 2026

works as expected.

elhumbertoz added a commit to elhumbertoz/whatsapp-web.js that referenced this pull request Apr 10, 2026
@BenyFilho BenyFilho added the approved Confirmed by maintainers label Apr 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api changes API modifications approved Confirmed by maintainers utility Utility code waiting for testers Needs testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Client.getContacts() returns a list of empty objects

5 participants