[SOLVED] Problem with Redis when migrating server to self hosted

For Redis a chose this option of the guide: ā€œB. Use hosted Redis Enterprise Cloud (hosted)ā€.
I followed it but I dont know how to know where the redis instance is running. Where should I check?

You should get the redis connection string directly from some interface

Where can I get that from?

In stackoverflow I have found that this may solve my problem:

let redisClient = redis.createClient({
 url: 'redis://redis:6379',
 legacyMode: true,
});

In which script of the parse-server-migration demo should I include this code?

you have to set a redis first somewhere and from there to get that connection string that is similar to this one:

With setting a redis you mean I have to install it folllowing this guide: https://redis.io/docs/getting-started/installation/install-redis-on-windows/ ?

Or just with copying this connection (ā€œredis-cli -u redis://:@redis-17335.c12.us-east-1-4.ec2.cloud.redislabs.com:17335ā€) I got from redis and pasting it to the env. script in the parse-server-migration demo will work?

You need a string like this one

You can follow this tutorial - it shows what you need to put into your .env file.

Okey I followed this guide and managed to set up the server succesfully, thank you!

But now I am having trouble with the client setup, connecting my dapp to this server. Before it worked perfectly, using a Moralis Dapp server and connecting it to my dapp in the index.js file using:

const serverUrl = "taken from Moralis dapp"; 
    const appId = "taken from Moralis dapp"; 
    Moralis.start({appId,serverUrl });

Now after creating my self hosted server I am using the same code, but replacing the serverUrl and appId values:

const serverUrl = "http://localhost:1337/server"; 
    const appId = "taken from value set in .env file from parse-migration-server demo"; 
    Moralis.start({appId,serverUrl });

The problem is that when I try to authenticate with metamask, metamask window appears, but when I try to log in I get the following error:

ā€œPOST http://localhost:1337/server/users 404 (Not Found)ā€

Did I do anything wrong? What should I change or try? Thank you!!

there is a different code that you have to use for authentication when you self host, you will find examples in the tutorial

Okey so I did ā€œMoralis.startā€ correctly but need to change authentication now?

this is how the authentication has to be used with a self hosted sever

you can also try to change the default timeout for requestMessage from 15 to a higher value

I am using this code for the self hosted server, but my dapp is not working. Before when I used it with a Moralis server it did work, so maybe I am missing something like importing a package or Moralis version.
I am getting this errors:
"Uncaught (in promise) Error: XMLHttpRequest failed: ā€œUnable to connect to the Parse APIā€
at handleError
ā€œauthentication.js:12 Uncaught (in promise) ReferenceError: account is not definedā€
Here is my code simplified to address this issue:
HTML

  <head>
 <script src="https://cdn.jsdelivr.net/gh/ethereum/[email protected]/dist/web3.min.js"></script> 
 <script src="https://unpkg.com/moralis-v1/dist/moralis.js"></script>
 </head>
<body> <button class="menu-btn-login" id="menu-btn-login">Connect Wallet</button>
    <script src="authentication.js"></script>
</body>

JS

/* ----0.Initializing---- */
    const serverUrl = "http://localhost:1337/server"; 
    const appId = "Jensen1620ID"; 
    Moralis.start({appId,serverUrl });
    const web3 = new Web3(window.ethereum); 

/*----1.Authentication----*/
async function login(provider) {
    // Enable web3 to get user address and chain
    await Moralis.enableWeb3({ throwOnError: true, provider });
  
    if (!account) {
      throw new Error('Connecting to chain failed, as no connected account was found');
    }
    if (!chainId) {
      throw new Error('Connecting to chain failed, as no connected chain was found');
    }
    
    // Get message to sign from the auth api
    const { message } = await Moralis.Cloud.run('requestMessage', {
      address: account,
      chain: parseInt(chainId, 16),
      network: 'evm',
    });
    
    // Authenticate and login via parse
    await authenticate({
      signingMessage: message,
      throwOnError: true,
    });
  }
document.getElementById("btn-login").onclick = login('metamask');

this is the account that is undefined?

Yes, i would like to be the account of the user that clicks the login button from my dapp. Before I did so using:

 user = await Moralis.User.current();
address=user.get("ethAddress"); 

But I do not know how to do that using the Auth Api.

This should be await Moralis.authenticate(...) for Vanilla JS.

account doesnā€™t seem to be declared in your code. You can look at this example.

How you solved the Redis connection issue im facong same issue can you please help me in this?

Hi, I followed this tutorial step by step and it worked fine for me

1 Like