Wishlist code works BUT!

Hello guys, wish you all having a great day :slight_smile:

I’m having a very little problem with my wishlist code and i don’t seem to know where is the problem. Would appreciate some help here. Thanks
in short: I have a repeater that displays a dynamic content form a database collection with a SWITCH in the container that allows the user to save items displayed to their own wishlist.

The problem :
Only one user can save an item to their wishlist! ( Who ever gets their first :smiley: )
which means an Item gets added only one time to only one user’s wishlist.

scenario :
User ( A ) browsing and then stops to save Item ( 1 ) to his wishlist . Result > DONE
User ( B ) browsing and then stops to save ( The same item - 1 ) to his wishlish Result > Nothing Happens.

My Code :

import wixData from 'wix-data';
import wixUsers from 'wix-users';
export function switch2_change(event, $w) {
 const user = wixUsers.currentUser; 
 const userId = user.id; 
 const itemId = event.context.itemId;
    wixData.query("feed") 
    .eq("_id", itemId)
    .find()
    .then((results) => {
 const itemToWishlist = results.items[0];
        itemToWishlist.userId = userId;  
        wixData.insert("ws", itemToWishlist) 
        .then(() => {
            console.log('Done')
        });
    });
 
}
  • My wishlist database collection (ws)
  • wishlist collection fields : userId ( Main Field )
    I added no more fields. only after i tested the code I did ( Synced ) all items from live to sandbox which resulted in adding new fields to the collection that I then Defined.
  • Permissions: Anyone can read, only a site member can create/update/delete

looking forward for your help here guys . Thank you

I believe the problem is that you’re not using the Repeated Item Scope on your switch2_change() event handler. See the Switch Component API for more information regarding the scoped selector in a Switch event handler.

1 Like

Thank you @yisrael-wix . I went to the page repeated Item Scope and I’m thinking yes this makes sense.
I’m Still trying to figure it out with my beginner coding experience . But I’ll very appreciate if you can help me more on implementing this to my code.

Guys I’m still trying to figure out how to implement what Yisrael suggested. I will very appreciate any help possible. thank you

@roi-bendet hello Roi, You wrote a comment here about this code. Can you help with my issue ?

To jump in quickly too, assuming that the Wix Stores Corvid sample for the wishlist is no good for you.
https://www.wix.com/corvid/example/wishlist

however, have you seen Nayeli (Code Queen) own example of a wishlist?
https://www.youtube.com/watch?v=RWgnjUQU7Ac - video of tutorial
https://codequeen.wixsite.com/wishlist - code is found through yellow ‘view the code’ text link in the footer of this page.

import wixUsers from 'wix-users';
import wixData from 'wix-data';
​
$w.onReady( () => {
  if(wixUsers.currentUser.loggedIn) {
    $w("#addtowishlist").label = "ADD TO WISH LIST";
  }
  else {
    $w("#addtowishlist").label = "SIGN IN TO SAVE";
  }
} );
​
export function addtowishlist_onclick() {
    wixData.insert('wishlist', 
    {wishlist_item: $w('#Itemdetails').getCurrentItem()._id}),
    $w('#success').show(),  //These are extra lines in case you have buttons or other
    $w('#addtowishlist').hide(),  //These are extra lines in case you have buttons or other
    $w('#viewlist').show();  //These are extra lines in case you have buttons or other
}

Both of these might be of no good use to your, however they are there if you want to have a quick look at.