Help with error connecting Moralis and MongoDB

no, you create a loop to iterate the result array, every element in that array has all this data, including the token_id

I apologize for my lack of knowledge, but performing the iteration this way, wouldn’t the database end up saving the same information for atributes like “owner_of” several times? Since in this case the user would have in the same wallet different tokens.

yes, that will happen, there will be multiple entries in the database for the same owner_of

I understand now, but even though I create a loop it still sends only one information to the database…

 for (var i=0; i < nftList.raw.result.length; i++) {
        const owner_of = nftList.raw.result[i].owner_of
        const block_number = nftList.raw.result[i].block_number
        const token_address = nftList.raw.result[i].token_address
        const token_id = nftList.raw.result[i].token_id

        const userdb = {owner_of, token_id, block_number, token_address}
        
        await connectDB();
        const MongoUser = await Users.findOne(
            {userdb: userdb},
        )

        if(!MongoUser){
        
        const newUser = new Users({
            owner_of: owner_of,
            token_id: token_id,
            block_number: block_number,
            token_address: token_address,
        })
        await newUser.save();
        } 
        console.log(nftList.raw.result[i])
    }

You can see that in the database it has only one entry:

try to add some logging now to see every element in that for loop

you can also move this one outside of the loop

I thought this would already shown me the elements inside the loop:

And if this is correct, It shows me the whole nftList.raw.result (for both tokens), but when I go to the database there is only one of the tokens there.

Note: When I use console.log(userdb) I can also see that it identifies the information, but it seems like is not sending to the database the second one. Here:

try to add more logging, to see exactly what you try to add to the database

When I use:

console.log(MongoUser)

I can see there is something wrong because it is only sending the first token two times:

try to add more logging, it should be something somewhere causing that

There is no other place where I can add logging… It just shows that on userdb it recognizes both of the tokens info from nftList. But, when it gets to MonguUser, something happens and it sends two times the same token_id…

you can add more logging, before you make the query, after the query

How can I make this?

you can use console.log on every line to see what happens