Help with async/await for .update [SOLVED]

I want to update multiple rows with the same user id.

I don’t understand how to implement the async /await function for it

This is what I tried

 
export function updateEmail_click(event, $w) {
 let user = wixUsers.currentUser; 
 let userId = user.id; //only for the rows having the same userId
 let values;
 const email = ($w('#input1').value); //this is the input value i want to insert
    wixData.query("Cart")
        .eq("userId", userId)
        .find()
        .then((results) => {
            values = results.items[0];
            let sum = email; //client's email here

            values.userId = userId;
            values.clientEmail = sum;
 
 //update only "value" field, but send all previous field as well
            wixData.update("Cart", values)   
                    .then((results2) => {
 let item2 = results2;
                    });
        });
}

I have multiple rows in the database with same user id

The Column name in the DB is ‘clientEmail’

Hey
When you use the update function you need to send the whole record to the function which you do. So far so good. Then I don’t understand the let sum = email and the text you wrote, update only value field? In your code you update all the value that you set, userId, clientEmail and no other.

I would console.log(results.items[0]); to make sure I get something to update before I run the update. I would also console.log(results2) to check what is actually updated.

let record = results.items[0]; 
record.userId = userId; // Don't know you want to update this field             
record.clientEmail = $w('#input1').value; 
wixData.update("Cart", record) .then((results2) => { console.log(results2); });

Hey Andreas,

How are you?

sorry for the confusion. Actually, I tried the code below:

export function updateEmail_click(event, $w) {
 let user = wixUsers.currentUser; 
 let userId = user.id; 
 let values;
 const email = ($w('#input1').value);
    wixData.query("Cart")
        .eq("userId", userId)
        .find()
        .then((results) => {
        values = results.items[0]; //old record
 let record = results.items[0];
            record.clientEmail = $w('#input1').value;
            wixData.update("Cart", record) .then((results2) => { console.log(results2); });
        });
}

The problem is below

You see, I have multiple similar ‘userId’ and I want the input value to be updated for each userId row

Do you get me ?

Thank a lot man!

Hey
First of all when Wix Code inserts data as [userId] that means that field does not exist in your data collection. So what is the key in the data collection in your Cart Data Collection?

Hey

The userId you see was already inserted in the previous page
I just want to update the empty Client Email ‘clientEmail’ column with the input value.

This is a shopping cart.

User inserted all the values you see (except for the Client Email) on the previous page & now when the user is on the Cart Page I want to capture their email address and update the rows accordingly before letting them checkout.

If you want to update more than one item you need to create a loop using .forEach or a ordinary for loop. You can only update one item at a time using the update function the way you use it.

I tried to loop as you said but still its updating only 1 entry

export function updateEmail_click(event, $w) {
 let user = wixUsers.currentUser; 
 let userId = user.id; 
 let values;
 const email = ($w('#input1').value);
    wixData.query("Cart")
        .eq("userId", userId)
        .find()
        .then((results) => {
            values = results.items[0];
 let mail = email;
 
 let items = results.items;
            items.forEach((item) => {
                values.clientEmail = mail;
                wixData.update("Cart", values)
            });
        });
}

You must use the item, not the values because the values you have only refer to one item using the [0]. So the update must use item to make each item update.

let items = results.items;             
items.forEach((item) => { 
                item.clientEmail = mail;
                 wixData.update("Cart", item) 
}); 
});

Thank you so much Andreas!

No problem, just happy I could be of assistance for you to continue using Wix Code. For more help and ideas don’t forget to signup wixshow.com and there you will get the Code Library.