Addition of Values to a Singular Field

I am relatively new to Wix, Velo, and Javascript and am creating a points system, in which I am currently having difficulty simply adding a member’s score value to a field in their record within a database.

I have tried using .update() (just to see if I could put a value into just one field) and have found that relatively nothing occurs, and expectedly, .insert() adds another item.
Using console.log, I know my code successfully records the value to a variable so I am unsure where my error is.
I have only included this function as this is all I have altered in an attempt to achieve my goal and haven’t found any other sections that influence.

The record is found through a query using the user’s email. The user is required to log in upon loading the page and their email is used in the ‘title’ field of the database.

import wixData from 'wix-data';
import wixUsers from 'wix-users';
import wixLocation from 'wix-location';
//I am including these to show what I have imported, in reality, a fair amount of code is inbetween here and the function
//=============================================================

export function button2_click(event) {
 let user = wixUsers.currentUser
 let userId = user.id
 let userEmail = wixUsers.currentUser.getEmail()
 
  wixData.query('members')
  .eq('title', userEmail)
    .find()
        .then(res => {
          if (res === 0) {
             // If email cannot be found in database
            }
          else {
             // If email is in Database
             let scoreUpdate = {
             // The following line is simply to see if I could change the value in a field (currently unsuccessfully)
             "points": scoreForQuiz
                }
                console.log(scoreForQuiz);
                wixData.insert("members", scoreUpdate)
                  .then( (results) => {
              let item = results;
                } )
                .catch( (err) => {
                    let errorMsg = err;
                } );       
            } 
        })
 }

My database’s permissions are set so anyone can read, create and update my collection.

Any guidance whatsoever toward my goal would be greatly appreciated, even if the method strays completely from the direction I have taken.

Thank you in advance.

Try to use ID-specified UPDATING.

Here an exerpt of the VELO-API for UPDATE…

The update() function compares the _id property of the specified item with the _id property values of the items in the specified collection. If an item in the collection has that _id value, update replaced the item’s property values with the ones in the specified item…
Here the link …
https://www.wix.com/velo/reference/wix-data/update

That means…you have to do something like…

let toUpdate = {
  "_id": "00001"
  "points": scoreForQuiz
};

You can take the found “_id” …

.then((res) => {
        console.log(res.items)
        let ID = res.items[0]._id
        console.log(ID)
})        
let toUpdate = {
  "_id": ID
  "points": scoreForQuiz
};

I am having some trouble following your suggestion.
I’ve modified my code accordingly and have found that the points field does in fact update but seems to delete every other field, including the ‘title’.

When I am assigning values to the “points” field should I then update the rest of the fields?
If so, who could I achieve this?
I’ve tried investigating the API Reference for update() and have only found guidance on updating specific strings.
Would I have to use the get() function to reassert the value of the fields?

export function button2_click(event) {

 let user = wixUsers.currentUser
 let userId = user.id
 let userEmail = wixUsers.currentUser.getEmail()

 
  wixData.query('members')
 //wixData.query($w("#dataset1"))
  .eq('title', userEmail)
    .find()
        .then(res => {
            console.log(res.items)
            let ID = res.items[0]._id
            console.log(ID)
            if (res === 0) {
              // If email is not in Database
            }
            else {
               // If email is in Database
                console.log(userEmail)
               let scoreUpdate = {
                    "_id": ID,
                    "points": scoreForQuiz,
                }
                wixData.update("members", scoreUpdate)
                  .then( (results) => {
                  let item = results; 
                } )
                .catch( (err) => {
                 let errorMsg = err;
                } );       
            } 
        })
 }

Thank you again for your assistance.

@brodietaylor2004

Perhaps this one…

import wixData from 'wix-data';
import wixUsers from 'wix-users';
import wixLocation from 'wix-location';
//I am including these to show what I have imported, in reality, a fair amount of code is inbetween here and the function
//=============================================================

 export function button2_click(event) {

 let user = wixUsers.currentUser
 let userId = user.id
 let userEmail = wixUsers.currentUser.getEmail()

   wixData.query('members')
   .eq('title', userEmail)
   .find()
   .then(res => {
         if (res === 0) {// If email is not in Database
         }
         else {// If email is in Database
              console.log(userEmail)
              console.log(res.items)
              let ID = res.items[0]._id
              console.log(ID)

              let scoreUpdate = results.items[0];
              scoreUpdate._id = ID
              scoreUpdate.points = scoreForQuiz;   
              console.log(scoreUpdate)  
              wixData.update("members", scoreUpdate); 
         } 
     })
 }

@russian-dima
Thank you, once again my goal has been reached because of your help.

@brodietaylor2004 :wink: You are welcome.

@russian-dima
Hello again, I’ve recently found that all changes occur to the top item of the database would you happen to know how to target a particular item?
I’ve experimented with a few things but my code is identical to your previous comment.

Yes you will have to expand your code and add a loop to loop trough found items.

Currently your code just works with the first found item…

let ID = res . items [ 0 ]. _id

Get the total number of found items…

itemsTOTAL = res.length (or res.items.length…or there is one more possibility how to get total count, something like .getTotalCount.)

After you have your total count, you do a loop from 0 to itemsTOTAL.:wink:

@russian-dima
Thank you again for your support.
A friend and I have been experimenting and trying to get our code to update a particular item in a database (identified through their email), but are currently seeing no results.
I believe the following if function below is incorrect as I have tried to print the value of userEmail to the console, only to have it come up as undefined, and the ELSE statement is executed instead :

if (res.items[i].title === userEmail)

I’ve also added a dataset to the page as the following line would not work without it.

let count = $w('#dataset1').getTotalCount()

=========================================================

export function button2_click(event) {

 let user = wixUsers.currentUser
 let userId = user.id
 let userEmail = wixUsers.currentUser.getEmail()

   wixData.query('members')
   .eq('title', userEmail)
   .find()
   .then(res => {
 if (res === 0) {// If email is not in Database
         }
 else {// If email is in Database
 console.log((userEmail).value)
 console.log(res.items)
 let count = $w('#dataset1').getTotalCount()
 for (var i = 0; i < count; i++) {
    let scoreUpdate = res.items[i];
    scoreUpdate._id = res.items[i]
    console.log(res.items[i].title)
    if (res.items[i].title === userEmail){
        console.log("Worked")
        console.log(scoreUpdate)
        scoreUpdate.points = scoreUpdate.points + scoreForQuiz + scoreForQuiz ;   
        wixData.update("members", scoreUpdate); 
        i = count + 1
     }
     else{console.log("Not Working")}
  }
 } 
 })
 }

Any guidance would be greatly appreciated.

This code-line seems to be strange…

scoreUpdate . _id = res . items [i]

@russian-dima
Thanks again, would you happen to know how I could get the PromiseResult of userEmail into a string?

Just to show what userEmail has been assigned as.

let userEmail = wixUsers.currentUser.getEmail()

I’ve tried just comparing the ‘res.items[i].title’ to a string I have put into the code manually and it has achieved exactly what I wanted but can’t seem to do the same for userEmail.
Using (userEmail).value didn’t appear to solve my problem
The code is identical to that of the above plus your observation.

I can’t seem to find a solution in API reference or within the community forum, so any guidance would again be much appreciated.

@brodietaylor2004 I think, this question is already answered, right?
https://www.wix.com/velo/forum/community-discussion/getting-a-string-from-a-promise