Inserting User Name From PrivateMembers into collection

Hi,

I’m using this code to insert a few values to a collection when a button is pressed. However, I’m trying to retrieve the “name” from Members/PrivateMembersData by creating a query within the variable data and then inserting this variable into the new collection on the field user but it just sends me empty curly brackets.

I’m a bit desperate already hahah Help!!!

Hi Andres,

You need to structure this differently to get the result you are seeking. The equals condition is not going to return anything. It needs to have the field followed by what value you are seeking. Then move the members query into the button click event. You have to wait for the query to execute in order to get the name value to insert into the collection.

export function applyCampaign_click(event) {
wixData.query("Members/PrivateMembersData")
  .eq("_id", userId)
  .find()
  .then( (results) => {
     if(results.items.length > 0) {
       let data = results.items[0].name;
       let toInsert = {"userId": userId, "campId":url, "user": data};
       wixData.insert("infCampReq",toInsert)
        .then( (insertResults) =>{
          $w('#submitNote').show();
        })
      }
  } )
  .catch( (error) => {
 let errorMsg = error.message;
 let code = error.code;
  } );
}

Ho anthony, I just tried with your code but it doesn’t send anything now to the collection and the #applyCampaign action is no longer working :confused:

Remember that the name field is a combination of first and last name, so both fields might need to be filled in for a result to be given.

Plus, when you use the Wix Members PrivateMembersData Collection.
https://support.wix.com/en/article/corvid-wix-members-privatemembersdata-collection-fields

You might need to suppress auth to get the function to work fully, this is mentioned in the Wix Data API and the Insert function.
https://www.wix.com/corvid/reference/wix-data.html#insert
https://www.wix.com/corvid/reference/wix-data.html#WixDataOptions

Or you can change your code to suit.
https://www.wix.com/corvid/forum/community-discussion/checking-if-member-exists-in-members-privatemembersdata
https://www.wix.com/corvid/forum/community-discussion/how-to-query-the-privatemembersdata-collection

Well, according to the API documentation the field Name (name) is the result of the combination from first and last name. So the field name should be “name” only. the query should be right, I don’t know why is not sending it.