Moralis API call problem

Hi. am folowwing Moralis web3 paypal clone tutorial at https://www.youtube.com/watch?v=IwfIxAJiNiw&t=4435s

when trying to listen to Moralis API calls to get data from smart contract, am typing “node index.js” then
on the browser http://localhost:3001/getNameAndBalance?userAddress=0x3Ba8FC5bCBfa77398aD6867c9dA7D889D682c6D7

instead of getting a response, it shows the following error on VS code terminal:

params: { _user, userAddress },
^

ReferenceError: _user is not defined

what do you think might be the problem? here is the code responsible for it:

app.get(’/getNameAndBalance’, async (req, res) => {
const { userAddress } = req.query

const response = await Moralis.EvmApi.utils.runContractFunction({
chain: ‘0x13881’,
address: ‘0x05095a8CcBD0d74c8e1aAF3bD201Cf70457fB399’,
functionName: ‘getMyName’,
abi: ABI,
params: { _user, userAddress },
})

const jsonResponseName = response.raw

const jsonResponse = {
name: jsonResponseName,
}

return res.status(200).json(jsonResponse)
})

1 Like

Hey @Coder91,

Thank you for reaching out to us :smiley:

Based on your error message, you have not defined the _user variable in your code as it is missing. You’ll just need to define a value for it to work. This is more of Javascript issue rather than Moralis API calls.

Let me know if this helps for you.

Hi, thx for reply. Well, I can see that it somehow thinks _user is not defined, however, I am copying this project from your tutorial repository right away, and then the tutor just adds a bit of code (I have posted it above) and then runs “node index.js” to see the API call. In my case it shows this error.

I’d much appreciate if you can give me a clue where to find and define this _user error. I have checked the code 10 times, exactly the same as in the video.

1 Like

here is the code again:

const express = require(‘express’)
const Moralis = require(‘moralis’).default
const app = express()
const cors = require(‘cors’)
require(‘dotenv’).config()
const port = 3001
const ABI = require(’./abi.json’)

app.use(cors())
app.use(express.json())

app.get(’/getNameAndBalance’, async (req, res) => {
const { userAddress } = req.query

const response = await Moralis.EvmApi.utils.runContractFunction({
chain: ‘0x13881’,
address: ‘0x05095a8CcBD0d74c8e1aAF3bD201Cf70457fB399’,
functionName: ‘getMyName’,
abi: ABI,
params: { _user, userAddress },
})

const jsonResponseName = response.raw

const jsonResponse = {
name: jsonResponseName,
}

return res.status(200).json(jsonResponse)
})

Moralis.start({
apiKey: process.env.MORALIS_KEY,
}).then(() => {
app.listen(port, () => {
console.log(Listening for API Calls)
})
})

1 Like

Solved!! haha, instead of params: { _user, userAddress } should be params: { _user: userAddress }

my bad… thx

2 Likes