[SOLVED] Help with API request

I just tried the solution and got the same error :cold_sweat:

I don’t know if this information can help in solving this problem, but when I try to put it “ConnectDB” inside the function triggered by the button, I also get an error, which is this one below:

And to try to make myself clearer, in that case the code would look like this with the “ConnectDB” function inside the trigger and not inside the “getServerSideProps” function:

...
            try {
                connectDB ();
                const userResult = await response.raw.result.find( (owner) => owner.owner_of == onwerad );
                console.log(userResult);
                const { Users } = require('../lib/userSchema')

                const owner_of = userResult.owner_of
                const token_id = userResult.token_id
                const amount = userResult.amount
                console.log(owner_of, token_id, amount);
                console.log("ok");

                const MongoUser = await Users.exists({owner_of: owner_of});

                if (!MongoUser) {
                    const newUser = new Users({
                        owner_of: owner_of,
                        token_id: token_id,
                        amount: amount,
                    })
                await newUser.save();
                console.log("everything works fine");
                }
                return user;
...

May be an issue with your schema? You are passing in two objects and the Mongoose docs only uses one.

If I take it off the “{ timestamps: true }” (the second object) from the code:

I got into a new error: :frowning_face:

This is a better error. I’m not sure if it will work properly but try doing mongoose.models?.users || ... as you’re trying to use either an existing model or a new one.

Did you follow a tutorial or page for this code?

Actually it is a mix from a template from one of the Moralis templates for getting user information as he log in on the website, but instead of sending information from the user as he is logged in on a page I want to get it when a button is trigger. And in the template all of this process is made on a “export async function getServerSideProps”, and in my case I it is not, because I need the button to be triggered to start the function

This change get into a new error

The error:
Captura de Tela 2022-11-09 às 22.46.05

Can you check this thread if any of the solutions work. Also try using the mongoose schema code in the same file for Next.js instead of importing it (to start with).

Hey glad, how are you!? I did find a solution but got into a problem with the Moralis API again… Now when I do the first request inserting the token_id and pressing the button the function calls the API and it works fine and send data from the user to the database. But when I try to do it again with a new token_id I receive an error from Moralis API:

Note: If I refresh the page and try to do the request again it works, but on the second time it gives me the error. It is always on the second try, and after that I can’t do any more requests because the error persists.

What was the solution that worked? In case someone else has a similar issue with mongoose.

Now when I do the first request inserting the token_id and pressing the button the function calls the API

What is this Moralis code you’re using now? Can you give exact examples e.g. token_id, address, chain, you’re using.

I had to use axios to send information from the page to other files that are working only on sending the files to the database.

The Moralis API code I am using right now is:

async function checkUser() {
        await Moralis.start({ apiKey: "xxxxxxx" });

        const response = await Moralis.EvmApi.nft.getNFTTokenIdOwners({
            address: '1234567890',
            chain: 5,
            tokenId: tokenid,
        });
...

Note: The tokenId is being set by the user on an input on the page and the request for getting information from the API is sent by a trigger button.

Maybe is there any command I can use to reset it or restart it after the first request? Because I think it has something to do with the fact that I am making a request again…

Can you check your network requests (in browser developer tools) to see what the request looks like when this error happens. Can you give exact examples e.g. token_id, address, chain, you’re using.

I found the error using the network request, thank you so much glad! The problem is solved! :grin:

1 Like