const addVotes = async (e) => {
const Posts = await Moralis.Object.extend("Posts");
const query = await new Moralis.Query(Posts);
query.equalTo("title", e.target.id)
const x = await query.first();
console.log(x)
if (x.get("votes") === undefined) {
x.set("votes", 0);
} else {
x.set("votes", x.get("votes") + 1);
}
x.save();
}
I have this code, where after each click, a vote gets added to a specific post, according to its title. The ID is the title of the post so I did the equalTo, the problem is that this code sometimes runs correctly and sometimes it shows get as undefined. I think the issue might be that the database takes time to load so you have to wait until clicking the button. Is there any way this can be fixed?