3

I am trying to fetch all accounts owned by my program using all() method but it is giving me this nasty error:

Uncaught (in promise) ReferenceError: Buffer is not defined
at AccountClient.all

Code:

const accounts = await program.account.user.all();

It works if I try to fetch a single account like this:

const account = await program.account.user.fetch(new PublicKey("..."));
4
  • 1
    try removing the brackets in .all() , await program.account.user.all(). Unless you're trying to use memcmp
    – john
    Mar 3, 2023 at 9:05
  • hey @john, I already tried the function without brackets. It is still the same error.
    – CryptoSar
    Mar 3, 2023 at 13:46
  • 1
    has the user account struct in your program been updated before? (ex. added new fields)
    – john
    Mar 3, 2023 at 20:30
  • Thanks @john. The error was indeed due to the addition of new fields. Some accounts were created with the old struct fields, hence the client wasn't able to deserialize the old accounts correctly. Can you write this as an answer so I can accept it?
    – CryptoSar
    Mar 4, 2023 at 9:02

1 Answer 1

3

Based on the comment above, it seems that the problem was due to the User account struct being modified in the program after some accounts were already created.

For instance, suppose the User struct was first defined in the following way, and several accounts were created:

#[account]
pub struct User {
pub user: Pubkey,
pub email: String,
}

Then, the program was updated to add a new field to the User struct:

#[account]
pub struct User {
pub user: Pubkey,
pub email: String,
pub name: String,
}

In this scenario, invoking await program.account.user.all() would result in an error because older accounts lack the new field and cannot be deserialized using the updated User struct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.