[SOLVED] Problems calling Moralis API

Could someone help me with this, I am trying to pass the owner wallet address via URL, for example “https://www…com/page?owner=owneraddress” and for this I have created a class component that I called User:

import { withRouter } from 'next/router';
import { Component } from 'react';

class ClassComponentExample extends Component {
    render() {
        const router = this.props.router;
        const { owner } = router.query;
        return (
            <p>{owner}</p>
        );
    }
}

export default withRouter(ClassComponentExample);

And when, as a test, I call this component in the page it brings me in text form the address sent by the URL:

However, when I use the same component inside the Moralis code to call the API, it gives error as invalid address:

import User from '../components/User';
....
the code I am using for this is(I hid the token address):
export async function getServerSideProps(context) {

    await Moralis.start({ apiKey: process.env.MORALIS_API_KEY });

    const nftList = await Moralis.EvmApi.account.getNFTsForContract({
        address: <User/>,
        tokenAddress: '...',
        chain: 5
    });

    return {
        props: {
            message:
                // if user has at least one NFT he will get protected content
                nftList.raw.total > 0 ? 'Yes' : "No",
            nftList: nftList.raw.result,
        },
    };
}

export default Protected;

Note: If I create the const called “owner” and put the wallet address in it and pass it by “address:” it works! And by that I mean:

const owner: "walletaddress"
...
    const nftList = await Moralis.EvmApi.account.getNFTsForContract({
        address: {owner},
        tokenAddress: '...',
        chain: 5
    });

I don’t understand why sending the address via URL is not being accepted by Moralis API… Could someone help me with this?

Thank you so much! =D

try do add some debugging to see if that variable has the expected value before passing it to Moralis.EvmApi.account.getNFTsForContract

Do you have any idea on how can I do this? Because, the only solution for debugging I could think was creating another page and watching what the <User/> Component was sending to it, and as you could see on the picture, it sends exactly what is write on the URL.

I thought that you can use console.log before calling that web3api function

I tried but it didn’t display anything different on console, it just show same error:

MoralisError [Moralis SDK Core Error]: [C0005] Invalid address provided

Maybe you didn’t run the updated code?

Can you look in network tab to see that values are sent to the http request?

This is the only error I get from Network tab:

But one thing I notice is that when I try to do it with declaring a “const owner” (typing in the address inside the code) it can find on the network tab a text with the address:

Maybe is there a different way to read a URL param like this, instead of using component? Do you know anything like this?

Because I am a beginner, so this was the only way I could think of to get this parameter that would work with the Moralis code template I found, but it is not working.

With this usage of a component and URL params - is there a reason for that instead of just using context directly in getServerSideProps to get this owner parameter?

export async function getServerSideProps(context) {
  const { owner } = context.query;

  const nftList = await Moralis.EvmApi.account.getNFTsForContract({
    address: owner,
    tokenAddress: '...',
    chain: 5,
  });

Hey Guys, I just solved the problem!
Thank you so much!! :pray: :pray: :pray: :pray:

What did you do? I’m having the same issue

What have you done in your code? Do you get any errors?

I’m getting the same error:
“isMoralisError: true,
code: ‘C0005’,
details: undefined,
[cause]: undefined”

My code is below, nothing has changed except that I wrapped it in a for loop to get the address value from an array. I’m testing with the same address that worked before, works directly in the https://docs.moralis.io/reference/getwallettokentransfers page, and is returning a plain string in console

console.log("AddressX: ", addressx);

    const response = await Moralis.EvmApi.token.getWalletTokenTransfers({
      addressx,
      chain,
    });

This doesn’t seem related to the use of Next.js API with Moralis? Can you make a new topic if that’s the case,or post more of your code if it is.

Ok posted new topic here MoralisError [Moralis SDK Core Error]: [C0005] Invalid address provided