[solved] API Issue with Moralis

Guys, I read the previous thread, and seems like I am not the only one having Moralis API call issues.

I completed youtube Moralis tutorials where we used Moralis API to fetch data from smart contract and show on front-end. So my app also works fine on localhost, but after creating production build and uploading to guthub and then to Internet, Moralis API calls disappear.

I went further and checked the console. It kept saying ‘axios’ issue. Well, then I looked at front-end App.js folder and indeed found the following:

async function getNameAndBalance() {
    const res = await axios.get(`http://localhost:3001/getNameAndBalance`, {
      params: { userAddress: address },
    })

so, it looks like axios is looking for localhost/3001 but since I am on Internet so he cant find any localhost and thats why is throwing an error…

Do you guys have any ideas about how I can change it? or what exactly should I change here. what code should I write to make axios work properly?

Unfortunately nothing about this has been mentioned in the video:(

Hope the dude from previous thread sees this and if mine gets fixed probably his will fix too.

thanks for help

you could try to change that localhost to the domain or IP where the server is running in case that you deployed it somewhere where it has a public IP or domain to access it, also the port may be different and not 3001

well, after spending 2-3 days on this issue, it started to work on the Internet. However it is still connected with my local VS code editor. Meaning, I have to open VS code, go to back-end folder, run ‘nodemon index.js’ command, and only then I get data fetched and shown on my website on the real server. The moment I close VC code or shut down computer, “chao bambina” hahaha it stops getting API calls!!! hahaha…

Any ideas please? I tried to change it instead of localhost:3001 to domain name but then it doesnt work because in back-end folder Moralis is set to run on port=3001.

really strange and complicated this moralis api stuff…tired)))

Well, issue is solved! It’s a pity that all Moralis “super” web3 developers were unable to answer my simple question, but who knows, maybe u guys are only good on paper))) Ivan definitely needs to update his team, if Moralis is to become a thing one a day)

To all those who wanna know how the issue was solved(in case u guys come across similar problem, which I’m sure most of u will), the only thing you have to do is to somehow make that nodeJS backend server run non-stop, so as your website once on real server, still get API calls from Localhost:3001 or whatever port u set it on. (otherwise once u close VS code, it shuts down). And the solution I found was:

Step1: Open terminal and in backend folder type command: install forever -g

Step2: In your back-end folder, find package.json file and under scripts add: “start”: “forever start index.js”

Step 3: again, go to backend folder and in terminal run command: forever start index.js

Good Luck guys!

In the console, I am getting the output which I expected, but I am not able to display the output in the frontend even though Metamask is connected.

Values are not displaying in frontend, But In console I am getting output


There is an error in getNameAndBalance. I guess also I Inserted updated API key and have been struggling for 5 days. Please help me out if you know the solution.

async function getNameAndBalance() {
const res = await axios.get(http://localhost:3001/getNameAndBalance, {
params: { userAddress: address },
});

Thanks in Advance

you can check server side to see what happens when that function getNameAndBalance is called