Backend Script Unable to Save to Private Database

Question:
I have a backend script that is unable to write to a private database in Live Mode.

Product:
Wix editor and Velo

What are you trying to achieve:
I am building a very simple password manager. I have simplified it to find the error. In this simplified code, I am attempting to save a username given by a user. They enter their username into a text box and click register. I use a backend file to access the database and save the username. In the preview mode, this process works whether the database is public or private. In the live mode, the backend file cannot access the private database.

What have you already tried:
I have tried using a different browser and clearing the cache of each browser. I believe that wicks automatically serves the website on HTTPS so I don’t think this is the problem. My code does not have any obvious errors.

Additional information:
Error Message:
Error saving username: Error: Unable to handle the request. Contact the site administrator or view site monitoring logs for more information.
(anonymous) @ createConsoleProxy.ts:47

Backend test.jsw
// test.jsw

import wixData from ‘wix-data’;

export function saveUsername(username) {
let toInsert = {
“username”: username
};

return wixData.insert(“TestUser”, toInsert);
}

If the database is set to private, it’s worth checking the permissions that are set for reading, writing, deleting and updating content.

Either way, you can use suppressAuth options when interacting with a database via code, although this should be used carefully, and security should be considered when using this option.

This is an example using dataOptions:

import wixData from 'wix-data';

// ...

let toInsert = {
  "_id":          "00001",
  "title":        "Mr.",
  "first_name":   "John",
  "last_name":    "Doe"
};

let options = {
  "suppressAuth": true,
  "suppressHooks": true
};

wixData.insert("myCollection", toInsert, options)
  .then((item) => {
    console.log(item); //see item below
  })
  .catch((err) => {
  console.log(err);
  });