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.
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;
} );
}
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.