Try to send only one parameter to that findOne function. You create an object with 3 keys and not 3 different objects
Is there a way that I can send all the token_ids? (If the user has more than one token)
what do you want to do? that is a query that. you are trying to do there
Well, I took it off other parameters to test if this was really the problem and it works. But when the app send to the database it only sends one token_id (the first one, in this case token_id: 0) but the user used for this test has also minted a token with the id 1. How can I detect both of the ids and send them to the database instead of just detecting and sending the token_id 0?
you can create more user objects with this syntax, and here instead of accessing only index 0 for first result you can iterate over all the results by checking the length of those result array (you will have to use pagination if there are more than 100 results)
Cryptokid, thank you very much for the help I just created a constant with all the objects I wanted to pass and was able to send to the database. However, I didnât understand very well how to detect and send more than one token_id, would you mind explaining me better how to do it or help me with an example using, token id 0 and 1, for example?
Here is how it looks right now:
const nftList = await Moralis.EvmApi.account.getNFTsForContract({
address: session.user.address,
tokenAddress: 'xxxxx',
chain: 5
});
const owner_of = nftList.raw.result[0].owner_of
const token_id = nftList.raw.result[0].token_id
const block_number = nftList.raw.result[0].block_number
const token_address = nftList.raw.result[0].token_address
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();
}
Note: I really understand if this is too much to ask so in any case I want to thank you very much for your patience and help, even if you canât help me with the how-to part for both tokens.
you have at least two options, you can change the type of token_id column to be able to handle an array (quite complicated to handle it), or you create multiple user objects (this one should be easier), one for each different token id
you will have to iterate over the results of
But if I choose the easier option(create multiple user objects), I would have to create a new object, and redeploy the app, each time I mint a new NFT with a new token_id, right?
you donât have to redeploy anything
here you can use the same object for findOne:
{
owner_of: owner_of,
token_id: token_id,
block_number: block_number,
token_address: token_address,
}
I am sorry Cryptokid, but I think I didnât understand what do you mean with creating multiple user objects, I thought you were saying that I should create different objects for each new token I mint using a new token id. And by that I mean:
const token_id2 = nftList.raw.result[0].token_id
const token_id1 = nftList.raw.result[1].token_id
const token_id0 = nftList.raw.result[2].token_id
...
So, if it was not that, I am sorry but I couldnât understand what you just purpose I should do. Would you mind explaining it to me better?
you are close, you have to use a for to iterate over those results (that result array where you process only index 0 now), based on the length of that array you get all the results components for index 0 then for index 1 and so on
I am trying to find an answer on the internet to make the loop inside this const, but not finding, do you have any idea of how could I do it?
You have to make a normal loop, not one inside a const
But if I do a normal loop, how could I then declare it inside the âuserdbâ without creating a const?
you donât have to use a const, you can use a normal variable without const
I found this on the internet:
for (var key of Object.keys(p)) {
console.log(key + " -> " + p[key])
}
Would it be right if I turn this code into, And then declare it as token_id on the âuserdbâ?
for (var token_id of Object.keys(nftList)) {
var token_id = (token_id + ":" + nftList[token_id])
}
Do I have setup this differently on Schema?
you donât have to change anything in the schema
try to search how to do a for for an array and not for an object
Is creating a class for token_id something close to the answer? I found this on a tutorial to iterate arrays:
class token_id extends Component {
constructor(props){
super(props)
this.state = {
data: []
}
}
getData(){
axios.get(nftList.raw.result).then(res => {
var data = res.data
this.setState({data : data})
})
}
componentDidMount(){
this.getData()
}
render() {
return (
<>
<ul>
{this.state.data.map(d => (<li key={d.token_address}>"token_id:"{d.token_id}</li>))}
</ul>
</>
)
}
}
And then i could insert this class inside the âuserdbâ
is has to be more simple than that, a simple for that iterates an array, search that on google
I am trying but I still could not find or visualize a solution that would fit in this situation⌠Because I need to create a loop that identify this âtoken_idâ and then declare this token idâs as a var, so that they can pass through the userdb, right?