So i am trying to build a shopping cart using code
this is the code for add to cart function:
export function container1_click(event) {
let user = wixUsers.currentUser;
let userId = user.id;
const itemId = event.context.itemId; // this is the item in the repeater assuming that the button is in the repeater.
wixData.query("TRIPOLI") // get the item from the database collection.
.eq("_id", itemId)
.find()
.then((results) => {
let saveit = results.items[0].title;
let toSave = {
"title": saveit,
"userId": userId // adding a field of the current user to the database in order to distinguish between other users wishlist.
};
wixData.save("Saveditem", toSave);
console.log(saveit);
});
}
now for checkout i need to remove the items that were added of (course all items)
but it is not removing anything from the database even though there is no error.
any help how i can remove everything associated with the user id from the database
?
export function button34_click() {
let user = wixUsers.currentUser;
let userId = user.id;
// const itemId = event.context.itemId; // this is the item in the repeater assuming that the button is in the repeater.
wixData.query("Saveditem") // get the item from the database collection.
.eq("userId", userId)
.find()
.then((results) => {
let remove = results.items[0];
wixData.remove("Saveditem", remove);
console.log(remove);
});
}
this is the database so when i click checkout i need it remove every row that matches the query
Hi,
Remove method receives two arguments.
The first one is the name of the database collection, like you did.
The second receives an id.
So, it should look like this:
export function button34_click() {
let user = wixUsers.currentUser;
let userId = user.id;
// const itemId = event.context.itemId; // this is the item in the repeater assuming that the button is in the repeater.
wixData.query("Saveditem") // get the item from the database collection.
.eq("userId", userId)
.find()
.then((results) => {
let remove = results.items[0]._id; // instead of results.items[0]
wixData.remove("Saveditem", remove);
console.log(remove);
});
}
Good luck!
Roi.
thanks worked
but it only removes one row not all the rows that has the userid .
every time i click it removes one row.
i need it to remove all the rows that has the same currentuser.id
Ok,
So I think async/await will be good solution for you.
Check this out:
export function button34_click() {
let user = wixUsers.currentUser;
let userId = user.id;
// const itemId = event.context.itemId; // this is the item in the repeater assuming that the button is in the repeater.
wixData.query("Saveditem") // get the item from the database collection.
.eq("userId", userId)
.find()
.then((results) => {
removeItems(results.items);
});
}
async function removeItems(items) {
items.forEach(async (item, i) => {
await wixData.remove("Saveditem", items[i]._id)
})
}
Please update in your progress.
Roi.
it works perfectly. Thank you so much.
this is the code to remove items from repeater if any one needs-
let newid= "";
export function negitive_click(event) {
let user = wixUsers.currentUser;
let userId = user.id;
const itemId = event.context.itemId; // this is the item in the repeater assuming that the button is in the repeater.
wixData.query("TRIPOLI") // get the item from the database collection.
.eq("_id", itemId)
.find()
.then((results) => {
let itemidd = results.items[0];
newid= itemidd.title;
wixData.query("Saveditem") // get the item from the database collection.
.eq("title", newid)
.eq("userId", userId)
.find()
.then((results) => {
let remove = results.items[0]._id;
wixData.remove("Saveditem", remove);
console.log(remove);
});
});
}
On what page may I copy & paste this code on so that items do not remain in cart?
Thanks