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

Hi I am following this guide (https://docs.moralis.io/docs/run-parse-server-locally) to run my server locally. I have followed all steps but when running “yarn dev” on the parse-server-migration folder I get the following error:

    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1161:16)
Emitted 'error' event on RedisClient instance at:
    at RedisClient.on_error (C:\Users\6inim\Documents\Blockchain\Wevelop\wevelop.io-v6-automate\Moralis-JS-SDK\node_modules\parse-server\node_modules\redis\index.js:342:14)
    at Socket.<anonymous> (C:\Users\6inim\Documents\Blockchain\Wevelop\wevelop.io-v6-automate\Moralis-JS-SDK\node_modules\parse-server\node_modules\redis\index.js:223:14)
    at Socket.emit (node:events:390:28)
    at Socket.emit (node:domain:475:12)
    at emitErrorNT (node:internal/streams/destroy:157:8)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  errno: -4078,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 6379
}
error Command failed with exit code 7.

The error has something to do with Redis. Regarding it I had some doubts following the guide with the “REDIS_CONNECTION_STRING”. In the guide it says that the end result should look like this (‘redis://:@’). I dont know what it means by this. I typed: ‘redis://username:password’, but I do not know if I did correctly. It may be that the error has nothing to do with this. In that case could you tell me what the error may be? Thanks and best regards.

It tries to connect to that localhost IP for redis, is there where the redis instance is running?

You also have to add the server url or ip in that string

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?