Database update isn't working

I am trying to update a database in order to calculate the value for some of the fields. So I used the database update API and then a refresh on the dataset to get the updated field to be shown on the page.

However, it’s not updating the database at all, when the item is returned in the console after the dataset refresh it shows the last update was 7th August and in the database itself, the fields remain blank - so the function doesn’t seem to be executing properly/at all.

If anyone could take a look and let me know what might be wrong that would be super helpful!

Here’s the page code:

import wixData from 'wix-data';

$w.onReady(function () {

    wixData.query("soccerCoedTable")
        .find()
        .then((results) => {
            if (results.items.length > 0) {
                let firstItem = results.items[0];
                let winPoints = firstItem.won * 3;
                let pointsTotal = winPoints + firstItem.drawn;
                let toUpdate1 = {
                    "team": firstItem.teamm,
                    "played": firstItem.played,
                    "won": firstItem.won,
                    "drawn": firstItem.drawn,
                    "lost": firstItem.lost,
                    "goalsFor": firstItem.goalsFor,
                    "goalsAgainst": firstItem.goalsAgainst,
                    "goalDifference": firstItem.goalsFor - firstItem.goalsAgainst,
                    "points": pointsTotal
                };
                let secondItem = results.items[1];
                let winPoints2 = secondItem.won * 3;
                let pointsTotal2 = winPoints2 + secondItem.drawn;
                let toUpdate2 = {
                    "team": secondItem.teamm,
                    "played": secondItem.played,
                    "won": secondItem.won,
                    "drawn": secondItem.drawn,
                    "lost": secondItem.lost,
                    "goalsFor": secondItem.goalsFor,
                    "goalsAgainst": secondItem.goalsAgainst,
                    "goalDifference": secondItem.goalsFor - secondItem.goalsAgainst,
                    "points": pointsTotal2
                };
                let thirdItem = results.items[1];
                let winPoints3 = thirdItem.won * 3;
                let pointsTotal3 = winPoints3 + thirdItem.drawn;
                let toUpdate3 = {
                    "team": thirdItem.teamm,
                    "played": thirdItem.played,
                    "won": thirdItem.won,
                    "drawn": thirdItem.drawn,
                    "lost": thirdItem.lost,
                    "goalsFor": thirdItem.goalsFor,
                    "goalsAgainst": thirdItem.goalsAgainst,
                    "goalDifference": thirdItem.goalsFor - thirdItem.goalsAgainst,
                    "points": pointsTotal3
                };

                wixData.bulkUpdate("soccerCoedTable", [toUpdate1, toUpdate2, toUpdate3])
                    .then((results) => {
                        let inserted = results.inserted; // 0
                        let insertedIds = results.insertedItemIds; // []
                        let updated = results.updated; // 2
                        let skipped = results.skipped; // 0
                        let errors = results.errors; // []
                    })
                    .catch((err) => {
                        let errorMsg = err;
                    });

            } else {
                // handle case where no matching items found
            }
        })
        .catch((err) => {
            let errorMsg = err;
        });

    $w("#soccerCoedTableDataset").onReady(() => {
        console.log("The dataset is ready");

        $w("#soccerCoedTableDataset").refresh()
            .then(() => {
                console.log("Done refreshing the dataset");
                wixData.query("soccerCoedTable")
                    .find()
                    .then((results) => {
                        console.log(results);
                    })
                    .catch((err) => {
                        let errorMsg = err;
                    });
            });
    });

});
1 Like
import wixData from 'wix-data';

$w.onReady(function() {start_myFunction();});

function myFunction {
    wixData.query("soccerCoedTable")
    .find()
    .then((results) => {
        if (results.items.length > 0) {//Here you get your RESULTS after QUERY..
           let firstItem=results.items[0]; 
           console.log("First-ITEM: ", firstItem); 
            //Here you do your updating calculations......
           let winPoints=firstItem.won * 3; 
           console.log("Win-Points: ", winPoints);
           let pointsTotal=winPoints+firstItem.drawn; 
           console.log("Points-Total: ", pointsTotal);
            
           // YOU JUST NEED THIS.....!!!!!!!
           //Updating values in your RESULTS-OBJECT here.....
           firstItem.won = winPoints;
           firstItem.points = pointsTotal
           // YOU JUST NEED THIS.....!!!!!!!

            
        // YOU DO NOT NEED THIS...... !!!!!!!!!!!!!!!!
        //  let toUpdate1 = {
        //        "team": firstItem.teamm,
        //        "played": firstItem.played,
        //        "won": firstItem.won,
        //        "drawn": firstItem.drawn,
        //        "lost": firstItem.lost,
        //        "goalsFor": firstItem.goalsFor,
        //        "goalsAgainst": firstItem.goalsAgainst,
        //        "goalDifference": firstItem.goalsFor - firstItem.goalsAgainst,
        //        "points": pointsTotal
        //    };
        // YOU DO NOT NEED THIS...... !!!!!!!!!!!!!!!!

          wixData.bulkUpdate("soccerCoedTable", [firstItem, /*secondItem,  ....*/])
         .then((results) => {$w("#soccerCoedTableDataset").refresh()
            let inserted = results.inserted; console.log(inserted);
            let insertedIds = results.insertedItemIds; console.log(insertedIds);
            let updated = results.updated; console.log(updated);
            let skipped = results.skipped; console.log(skipped);
            let errors = results.errors; console.log(errors);
         })
         .catch((err)=> {console.log(err);});
      }
      else {console.log("No RESULTS found!");}
   })
   .catch((err)=> {console.log(err);});
}

I now get this error:

public/pages/cylmj.js: Unexpected token, expected "(" (9:25)     
7 | );    
8 | 
>  9 | function calculatePoints {      
                                ^   
10 |     wixData.query("soccerCoedTable")   
11 |         .find()   12 |         .then((results) => {

@lisamthorpe
This is a typo-error → search for closing and opening brakets.

Okay never mind - it was just missing the “()” on the function.

This now works:

 function calculatePoints (){

@lisamthorpe :wink: Now everything works (like expected)?
Do not forget to like it if you really liked it.

@russian-dima Haha you’re a frickin’ star!

I added the calculation for goalDifference also.

Now I’m just wondering if there is an easier way to do this update for all of the items, or do I need to create a variable for each (firstItem, secondItem…) and run the code for each one?

Here’s the working code:

import wixData from 'wix-data';

$w.onReady(function ()

    { calculatePoints(); }

);

function calculatePoints (){
    wixData.query("soccerCoedTable")
        .find()
        .then((results) => {
            if (results.items.length > 0) { 
                let firstItem = results.items[0];
                console.log("First-Item: ", firstItem);
                //Updating calculations......
                let winPoints = firstItem.won * 3;
                console.log("Win-Points: ", winPoints);
                let pointsTotal = winPoints + firstItem.drawn;
                console.log("Points-Total: ", pointsTotal);

                //Updating values in RESULTS-OBJECT here.....
                firstItem.won = winPoints;
                firstItem.points = pointsTotal;
                firstItem.goalDifference = firstItem.goalsFor - firstItem.goalsAgainst;

                wixData.bulkUpdate("soccerCoedTable", [firstItem, /*secondItem,  ....*/ ])
                    .then((results) => {
                        $w("#soccerCoedTableDataset").refresh()
                        let inserted = results.inserted;
                        console.log(inserted);
                        let insertedIds = results.insertedItemIds;
                        console.log(insertedIds);
                        let updated = results.updated;
                        console.log(updated);
                        let skipped = results.skipped;
                        console.log(skipped);
                        let errors = results.errors;
                        console.log(errors);
                    })
                    .catch((err) => { console.log(err); });
            } else { console.log("No RESULTS found!"); }
        })
        .catch((err) => { console.log(err); });
}

@lisamthorpe
Think about a RETURN-Function!!!

You put something into your RESTURN-FUNCTION → and you get an RESULT.
Now try to modify your existing function which do the query and transform it into a → RETURN-Function.

Then you will be able just to do the INPUT of the right DB (which has to query) and the function returns all DATA of the wished DB (it returns the query).

Continue to do some →

@russian-dima Okay my brain is hurting but I’m going to try and figure it out haha.

It’s obviously something to do with this little number in here

letfirstItem=results.items[0];
                           ^

because I can’t just do “item” and it has to have an argument in there right…

@lisamthorpe

Your best friend is → CONSOLE <<---- use it

  1. Invastigate every code-line which you do not understand).
  2. Take always a look onto given results.
  3. Try to understand given results.
  4. Do some brainstorming what you want to be happen with the results.
  5. Think about how to achieve the next step!
  6. When you got stuck → ask papa-google :wink: (always helps).
  7. Just this way you will learn faster and more effective.

This is how CODING works → step by step.

The compiler also works the same way → step by step! (REPLAY)

@russian-dima I will have to come back to try and work out how to simplify it because it will take me some time.

I have done this for now (which updates all 4 items):

 function calculatePoints (){
    wixData.query("soccerCoedTable")
        .find()
        .then((results) => {
            if (results.items.length > 0) { 
                //Update first item
                let item = results.items[0];
                console.log("Item: ", item);
                //Updating calculations......
                let winPoints = item.won * 3;
                console.log("Win-Points: ", winPoints);
                let pointsTotal = winPoints + item.drawn;
                console.log("Points-Total: ", pointsTotal);

                //Updating values in RESULTS-OBJECT here.....
                item.points = pointsTotal;
                item.goalDifference = item.goalsFor - item.goalsAgainst;

                //Update second item
                let item2 = results.items[1];
                console.log("Item2: ", item2);
                //Updating calculations......
                let winPoints2 = item2.won * 3;
                console.log("Win-Points2: ", winPoints2);
                let pointsTotal2 = winPoints2 + item2.drawn;
                console.log("Points-Total2: ", pointsTotal2);

                //Updating values in RESULTS-OBJECT here.....
                item2.points = pointsTotal2;
                item2.goalDifference = item2.goalsFor - item2.goalsAgainst;

                //Update third item
                let item3 = results.items[2];
                console.log("Item3: ", item3);
                //Updating calculations......
                let winPoints3 = item3.won * 3;
                console.log("Win-Points3: ", winPoints3);
                let pointsTotal3 = winPoints3 + item3.drawn;
                console.log("Points-Total3: ", pointsTotal3);

                //Updating values in RESULTS-OBJECT here.....
                item3.points = pointsTotal3;
                item3.goalDifference = item3.goalsFor - item3.goalsAgainst;

                //Update fourth item
                let item4 = results.items[3];
                console.log("Item: ", item4);
                //Updating calculations......
                let winPoints4 = item4.won * 3;
                console.log("Win-Points4: ", winPoints4);
                let pointsTotal4 = winPoints4 + item4.drawn;
                console.log("Points-Total4: ", pointsTotal4);

                //Updating values in RESULTS-OBJECT here.....
                item4.points = pointsTotal4;
                item4.goalDifference = item4.goalsFor - item4.goalsAgainst;

                wixData.bulkUpdate("soccerCoedTable", [item, item2, item3, item4])
                    .then((results) => {
                        $w("#soccerCoedTableDataset").refresh()
                        let inserted = results.inserted;
                        console.log(inserted);
                        let insertedIds = results.insertedItemIds;
                        console.log(insertedIds);
                        let updated = results.updated;
                        console.log(updated);
                        let skipped = results.skipped;
                        console.log(skipped);
                        let errors = results.errors;
                        console.log(errors);
                    })
                    .catch((err) => { console.log(err); });
            } else { console.log("No RESULTS found!"); }
        })
        .catch((err) => { console.log(err); });
}

@russian-dima Okay so that all worked in preview mode, but it doesn’t seem to be working on the live site.

@lisamthorpe
What’s the difference between LIVE and PREVIEW?

—> Did you sync your LIVE and PREVIEW DATABASE ?
—> What about DATABASE-PERMISSIONS?

These 2 are in most cases the reason for an ERROR. Check it out.

All good now! You’re right, I had the dataset set to read and write but forgot to make the database itself read & write by anyone.

Thank you for all your help I really appreciate it!!

All good now! You’re right, I had the dataset set to read and write but forgot to make the database itself read & write by anyone.

Thank you for all your help I really appreciate it!!