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
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
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?
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.
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.
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.