I have been banging my head against the proverbial wall for a while now and I’m hoping someone here can help. I have (what I believe) is a rather simply page. The page should check for a user’s record in a database, if it doesn’t exist it’ll add the user to the database. That part works and I’m doing that with the following method:
let userEmail = currentMember . getMember (). then (( member ) => {
**let** userEmail = member.loginEmail ;
console . log ( "userEmail function: " + userEmail )
**return** member.loginEmail ;
})
. then (( userEmail ) => {
wixData . query ( “selfTapeMayParticipantData” )
. eq ( “email” , userEmail )
. find ()
. then (( res ) => {
console . log ( "res.length: " + res.length )
if ( res.length == 0 ) {
let toInsert = {
“email” : userEmail
};
wixData . insert ( “selfTapeMayParticipantData” , toInsert )
. then (( userEmail ) => {
$w ( “#dataLocalSelfTapeMayParticipantData” ). setFilter ( wixData . filter (). eq ( “email” , userEmail ));
**let** profItem = $w ( '#dataLocalSelfTapeMayParticipantData' ). getCurrentItem ();
})
} **else** {
$w ( "#dataLocalSelfTapeMayParticipantData" ). setFilter ( wixData . filter (). eq ( "email" , userEmail ));
**let** profItem = $w ( '#dataLocalSelfTapeMayParticipantData' ). getCurrentItem ();
}
})
})
The next step is to UPDATE the existing record with data captured on the page. I am choosing to do this via code because I want more flexibility down the road. Here is the code I am attempting to leverage and it simply won’t work. I have been fiddling around with different permutations and sometimes the _updatedDate record will update, but no data will show up in the expected now. Other times I get an error of some sort. Any guidance or help would be much appreciated.
export function saveData ( itemID ){
let userEmail = currentMember . getMember (). then (( member ) => {
return member.loginEmail ;
})
let instagramHandle = $w ( ‘#inputInstagram’ ). value
//console.log("instagramHandle " + instagramHandle)
console . log ( "instagramHandle value " + instagramHandle )
console . log ( "itemID: " + itemID )
/—OPTION #1—/
wixData . get ( “selfTapeMayParticipantData” , itemID )
. then (( item ) => {
// Update the instagram field of the item
item.instagram = instagramHandle ;
console . log ( "item.instagram: " + item.instagram )
// Save the updated item to the database
return item.save ;
})
. then (() => {
console . log ( "Item " + itemID + " updated successfully with value: " + instagramHandle );
})
. catch (( err ) => {
console . log ( "Failed to update item: " + err );
});
/*****OPTION #2 */
wixData . get ( “selfTapeMayParticipantData” , itemID )
. then ( dataCollection => {
let dataToSave = ({
“instagram” : instagramHandle
})
console . log ( “----dataToSave—” )
console . log ( dataToSave )
console . log ( “---------------” )
dataCollection . update ( dataToSave )
console . log ( “----dataCollection—” )
console . log ( dataCollection )
console . log ( “---------------” )
return dataCollection.save ;
})
/*****END OPTION #2/
}