Issue with this code

Hi moderators, i have an issue which is displayed below :-1:

Uncaught ReferenceError: moralis is not defined
at main.js:1 also the code below is from main.js :-1:

moralis.initialize("");

moralis.serverURL = “”;

async function initializeApp(){

 let currentUser = moralis.User.current();

 if(!currentUser){

     currentUser = await moralis.Web3.authenticate();

 }

 alart("user is signed in")

}

initializeApp();

there is a new syntax now for latest Moralis SDK:
https://docs.moralis.io/moralis-server/getting-started/quick-start#initialize-moralis

const serverUrl = "https://xxxxx.grandmoralis.com:2053/server";
const appId = "YOUR_APP_ID";
Moralis.start({ serverUrl, appId });

also you may need to add this to your html:

<head>
  <script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
  <script src="https://unpkg.com/moralis/dist/moralis.js"></script>
</head>

hey, i am facing the same issue. can ou pls explain where do I have to change the code.(Non tech background)

> <html>
>   <head>
>     <!-- Moralis SDK code -->
>     <script src="xx"></script>
>     <script src="xx"></script>
>   </head>
> 
>   <body>
>     <h1>Try it once</h1>
>     <button id="btn-login">Moralis Login</button>
>     <button id="btn-logout">Logout</button>
>     <script>
>   // connect to Moralis server
>   Moralis.initialize("xxx");
>   Moralis.serverURL = "https://wjdserver";
> 
>   // add from here down
>   async function login() {
>     let user = Moralis.User.current();
>     if (!user) {
>       user = await Moralis.authenticate();
>     }
>     console.log("logged in user:", user);
>   }
>  initializeApp();
>   async function logOut() {
>     await Moralis.User.logOut();
>     console.log("logged out");
>   }
> 
>   document.getElementById("btn-login").onclick = login;
>   document.getElementById("btn-logout").onclick = logOut;
> </script>
>   </body>
> </html>

the code is not opening metask

what do you have here instead of those src="xx"?

what does this do, I don’t see where you defined this function.

You can look in your browser console to see if there are any error there.

instead of xx i used my app id and server url

1 Like
<script src="xx"></script>

<script> Imports js file or library https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script . It’s not for Moralis initialization.

Your code should be like this:

> <html>
>   <head>
>     <!-- Moralis SDK code -->
>     <script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
      <script src="https://unpkg.com/moralis/dist/moralis.js"></script>
>   </head>
> 
>   <body>
>     <h1>Try it once</h1>
>     <button id="btn-login">Moralis Login</button>
>     <button id="btn-logout">Logout</button>
>     <script>
>   // connect to Moralis server
      const serverUrl = "https://xxxxx/server";
      const appId = "YOUR_APP_ID";
      Moralis.start({ serverUrl, appId });
> 
>   // add from here down
>   async function login() {
>     let user = Moralis.User.current();
>     if (!user) {
>       user = await Moralis.authenticate();
>     }
>     console.log("logged in user:", user);
>   }
>  initializeApp();
>   async function logOut() {
>     await Moralis.User.logOut();
>     console.log("logged out");
>   }
> 
>   document.getElementById("btn-login").onclick = login;
>   document.getElementById("btn-logout").onclick = logOut;
> </script>
>   </body>
> </html>

Apologies for the cross post but wanted to add to this thread as I was having a related issue, and this is detailed here:

Where the code does nothing on Chrome or Brave but logs me in on Firefox. Once logged in on FF I can see the details in my server dashboard. But the logout button is unresponsive and does not function.

Can you add some screen shoots and the link from GitHub with the code that you use?

The code is the exact code from the Vanilla Boilerplate here
[https://github.com/MoralisWeb3/demo-apps/tree/main/moralis-vanilla-boilerplate].
Plug in the key and server, it should work. And it connects on FF so it “sort of” works. I’d do a screenshot but you can see it on this link.

we can continue in only one thread and not in two forum threads

Yes I was only adding to this thread in case other people found this in search but didn’t want this thread to end in a dead end. Will continue in the other one!

Hi, i am having an issue with the ethereum boilerplate when ever i tried yarn start i get this
yarn start
yarn run v1.22.17
error Couldn’t find a package.json file

you can post this question in the ethereum boilerplate specific forum thread: Ethereum Boilerplate Questions

you can also read there, maybe someone else already had your problem