Save boolean to collection

$w(“#button5”) is on a page where a user will click it once they have completed a course. I’m having trouble trying to get the button to save “true” to a boolean field in a collection named “Courses”. I’ve searched for similar issues/read the API reference but can’t figure out why it won’t save. Also for security sake (so users can’t skip the course before submitting), would it be worth having the function run in the events.js file rather than on the front end? Thanks

import wixData from 'wix-data';
import wixUsers from 'wix-users';
import wixLocation from 'wix-location';

let user = wixUsers.currentUser;
let userId = user.id;

$w.onReady(function () {

    wixData.query("Courses")
        .eq("_id", userId)
        .find()
        .then((results) => {

             let toInsert = {
             "_id": userId,
             "baristaBasics": results.items[0].baristaBasics,
             "hospitality101": results.items[0].hospitality101,
             "baristaComplete": true,
             "hospitalityComplete": results.items[0].hospitalityComplete
             //"baristaDate": currentDate
            };
            $w("#button5").onClick((event) => {
                    if (results.items[0].baristaComplete !== true) {
                        wixData.update("Courses", toInsert)
                        $w("#button5").label = "Changed"
                    } else {
                        wixLocation.to('/account/my-courses')
                }
            })
        })
})

bump

Please note that according to the Corvid Community Guidelines , it is not permitted to bump posts.

What exactly is happening? What’s working? What isn’t?

Sorry I didn’t realise.
It won’t update toInsert to the Courses collection. It’s supposed to query the Courses collection based on current user’s Id, get the current values for each field in the collection, then update a true boolean, along with the current values. When published and the button is clicked, the collection is unchanged. I‘ve read through all the documentation for wixData.query and .update so I think my code should be correct but must have made a mistake and can’t figure out where. Sorry if that doesn’t make sense, and thanks for your help

Does it work in Preview? If so, then the problem might be collection permissions , or that you didn’t sync your database .