[SOLVED] This function works in console but not if called in .js file

setNewItemData1 function question.

hello. This function works just fine when I call it from the console after entering a number in the html page for the Tunacan.price, as it updates the moralis object database perfectly.

but WHY does the function NOT work when it is called separately within the .js file? I tried calling it in the file like I have illustrated in the //commented out line. Nothing happens. I checked to make sure that Tunacan.price was updated, but the moralis database does not update. Plus I get this message:

moralis.js:23794
Uncaught (in promise) Error: You need to call Parse.initialize before using Parse.
at Object.generatePath (moralis.js:23794)
at Object.currentInstallationId (moralis.js:1317)
at Object.request (moralis.js:23275)
at Object.find (moralis.js:19621)
at ParseQuery.value (moralis.js:17694)
at setNewItemData1 (itembancentry.html:280)
at itembancentry.html:286

Thank you in advance for your review.

<script>
  setNewItemData1 = async () => {
  const query = new Moralis.Query('Items');
  query.equalTo("name", "tunacan")
  const tunacan = await query.first();
  if(tunacan){
    tunacan.set("price", Tunacan.price);
    await tunacan.save();
}
}
  //setNewItemData1();
</script>

this error says that you need to initialise Moralis first with application ID and SERVER_URL

Does this need to be done on every .js page that accesses Moralis? It is initalized on the main.js page…

maybe you load main.js after you run this code that you added in that block

Right now main.js is called after the code is run…

that is not how you want to do it if your code depends on what is executed in main.js how it is the initialisation in this situation

After login, the an html page is loaded for entry of prices for items that are rendered as objects with a price attribute in the Moralis database. The js script is also on the html page.
The main.js file is loaded as src at the bottom of the html page. The main.js file has the auth info.

in your particular case when you tested

<script>
  setNewItemData1 = async () => {
  const query = new Moralis.Query('Items');
  query.equalTo("name", "tunacan")
  const tunacan = await query.first();
  if(tunacan){
    tunacan.set("price", Tunacan.price);
    await tunacan.save();
}
}
  //setNewItemData1();
</script>

if you want to run setNewItemData1(); before main.js is loaded then it would not work because it depends on code from main.js.

Thank you I’ll test and revert back Monday.

Ok I tried inserting the main.js before the code and I even tried inserting it in the header…. No luck… the code works and updates the Moralis database only if the function is executed from the console.

Thanks in advance for looking at this!

you can insert that function in main.js and call it from inside main.js at the end of the script

Ok I’ll try that thanks

I moved the call to the moralis database into the same function that takes the input from the html page and it works… I really have no idea why but as long as it works… !!!
so you can close this case if you wish