okay so essentially I want to add my current user that is logged into a multi reference field of another user once they press a follow button on that users profile.
So if I want to follow your account, I am trying to use insertReference() to insert a reference of my profile item into the followers field in your item.
Here's my current code.
I iterate through each item in the repeater and set its individual components to build the individual users "mini profile"
itemdata refers to the current item.
$w("#repeater2").forEachItem(($item, itemData, index) => {//repeating user
let belt = itemData.belt;
let a=" | | | | ";
if (itemData.stripe==="0"){ a="`````````";}
else if (itemData.stripe==="1"){a="`|```````";}
else if (itemData.stripe==="2"){a="`| |`````";}
else if (itemData.stripe==="3"){a="`| | |```";}
else if (itemData.stripe==="4"){a="`| | | |`";}
here i'm setting their profile up. nothing special $item('#repeatedName').text=itemData.name; $item('#repeatedProfilePic').src=itemData.photoProfile; $item('#repeatedContainer').src=itemData.photoCover; $item('#repeatedBoxBelt').style.backgroundColor = session.getItem(belt); $item('#repeatedStripes').text = a; $item('#repeatedFollowButton').onClick((event)=>{
when the button is pressed i call the following function which i have created and i pass in the current profiles itemData followUser(itemData); }); });
export function followUser(itemData){ //must add current user(logged in user) to selected users followers //inserts a reference to the item with ID of the current user in the followers field of the item in the USER_ProfileData collection with the ID of the selected user(the one the current user wants to follow).
wixData.insertReference("USER_ProfileData", "followers", itemData._id, wixUsers.currentUser.id) .then( () => { console.log(">Reference inserted "); } ) .catch( (error) => {
console.log(error); } ); }
When I press the follow button I am getting the following printed to the console.
Error: Provided relationshipAttribute [followers] is not a multi-reference field.
The field is in fact a multi reference field...
just in case you want to look around. HERES THE LINK!
www.grapplehub.net
THIS PROBLEM HAS BEEN BUGGING ME FOR A LONG TIME!! ANY ADVICE IS VERY MUCH APPRECIATED!
There is a slight difference in that message, however we would need to see the rest of it to make sure that nothing else has been left off the end of it that could provide more help.
Error: Provided relationshipAttribute [followers] is not a multi-reference field.
The field is in fact a multi reference field...
As for the error, is it a simple case of overthinking things and not needing to use the insertReference function and to simply use the insert function instead?
insertReference
Inserts a reference in the specified property.
https://www.wix.com/corvid/reference/wix-data.html#insertReference
The insertReference() function returns a Promise that resolves when a reference to the referenced item(s) is added to the referring item in the specified property. The Promise is rejected if the current user does not have update permissions for the collection.
insert
Adds an item to a collection.
https://www.wix.com/corvid/reference/wix-data.html#insert
The insert() function returns a Promise that resolves to the inserted item after it has been added to the specified collection. The Promise is rejected if the current user does not have create permissions for the collection or the specified item includes an _id property whose value matches an existing ID in the collection. Meaning, insert() cannot overwrite an existing item in the collection.
See previous post and reply from Giri about it here.
https://www.wix.com/corvid/forum/community-discussion/wixdata-insertreference-not-working-with-single-reference-field
Also, see this Stack post too.
https://stackoverflow.com/questions/52820065/how-to-insert-reference-by-using-wix-code
Finally, see Wix's pages about using reference fields.
https://support.wix.com/en/article/about-reference-fields-in-database-collections
The value that is displayed in the reference field comes from the Main field of the specific item in the referenced collection. The actual value that is stored in the database is the ID of the referenced item (in other words, the value in the ID system field of the referenced collection).
https://support.wix.com/en/article/displaying-content-from-multiple-database-collections-using-reference-fields-4034931
Also about using multiple items in reference fields.
https://support.wix.com/en/article/working-with-multiple-item-reference-fields
https://support.wix.com/en/article/about-referencing-multiple-items-in-one-field
@kylered17 Hey have you managed to find a solution ?