Back

[SOLVED] Updating Array Returns Error

  • 1
  • Web
RangerDev
21 May, 2023, 21:02

I am trying to update a document which has an array attribute called Upvotes This is where it stores all the users that have upvoted the doc. and I am using this function the handle it.

TypeScript
function upvote(docID: any) {
    const promise = appwriteDatabases.updateDocument(DatabaseID,CollectionID,DocumentID,{
        'Name': docID.Name,
        'Tagline': docID.Tagline,
        'Description': docID.Description,
        'Link': docID.Link,
        'Free': docID.Free,
        'LaunchDate': docID.LaunchDate,
        'Author': docID.Author,
        'Icon': docID.Icon,
        'Thumbnail': docID.Thumbnail,
        'Authoruid': docID.Authoruid,
        // add the user's uid to the Upvotes array
        'Upvotes': [...docID.Upvotes, uid]
    });

    promise.then((response) => {

        console.log(response);
        console.log(docID)

    }, (error) => {

        console.log(error);

        alert(error.message);

    });

}

The error: UpvoteBtn.2e957bcf.js:1 Uncaught TypeError: e.Upvotes is not iterable at h (upvoteBtn.2e957bcf.js:1:778) at HTMLButtonElement.m (upvoteBtn.2e957bcf.js:1:934) h

Help appreciated!

TL;DR
Someone is trying to update an array attribute called "Upvotes" in a document using a specific function. The error message "e.Upvotes is not iterable" suggests that the "Upvotes" array might be empty. To solve this issue, they can initialize the "Upvotes" attribute with an empty array before updating it. They can modify the code to include the following line before the update: ```js 'Upvotes': docID.Upvotes || [] ``` This will ensure that the "Upvotes" attribute is always iterable, even if it is initially empty.
Binyamin
21 May, 2023, 22:44

Can you log and share the value in docId.Upvotes?

Binyamin
21 May, 2023, 22:44

Maybe your Upvotes is empty?

RangerDev
21 May, 2023, 22:45

the Upvotes array is empty

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more