How can you make the database loading faster?

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?

What you have here when you get undefined?

i get undefined in it

How often it happens? Or in what circumstances?

you need to wait some minutes before clicking another time, so I wait, click once and it upvotes, but then i need to wait another time because if not, it gives me undefined

So first you have no votes, on first click you set votes to 1. And then after few clicks you set votes back to 1 because it returned undefined?

no, first you have zero votes, on first click you set votes to 1, on second cick you set votes to 2. But after each click you need to wait n interval of time because if not, you get undefined. You know?

At this if, it generates an error because x is undefined?

I tested now with this code:

addVotes = async (title) => {
        const Posts = await Moralis.Object.extend("Posts");
        const query = await new Moralis.Query(Posts);
        query.equalTo("title", title)
        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();
    }

and I didn’t have any problem with undefined

yes but this doesn’t get you the specific post that you are trying to upvote. You know? It is like this, so it needs to detect the specific post

can you add a console.log(e.target.id) to see if you have what you expect there every time?

it does sometimes give me the result and sometimes it is completely blank… :thinking: