How can you add a new column to an existing class?

const addVotes = async (result) => {

        console.log(result.target.id)

        const Posts = Moralis.Object.extend("Posts");

        const query = await new Moralis.Query(Posts);

        query.equalTo("title", result.target.id);

        console.log(query)

        const x = await query.find()

        const results = JSON.stringify(x)

        const finalResults = JSON.parse(results)

        console.log(finalResults)

        finalResults[0].set("votes", votes++)

        finalResults.save()

    }

So right now this should work? :grimacing:

No, not a save like that on array, you have to make that save on the array element, similar on how you want to use .set

arrgh

this is fine then:

But when I do this, the finalResults doesn’t get saved as the item clicked, but whenever I console.log(finalResults) without the save and set it does print the correct object array.

const addVotes = async (result) => {

        console.log(result.target.id)

        const Posts = Moralis.Object.extend("Posts");

        const query = await new Moralis.Query(Posts);

        query.equalTo("title", result.target.id);

        console.log(query)

        const x = await query.find()

        const results = JSON.stringify(x)

        const finalResults = JSON.parse(results)

        console.log(finalResults)

        finalResults[0].set("votes", votes++)

        finalResults[0].save()

    }

I think that you can use x directly without these 2 lines

const addVotes = async (result) => {

        const Posts = Moralis.Object.extend("Posts");

        const query = await new Moralis.Query(Posts);

        query.equalTo("title", result.target.id);

        console.log(result.target.id)

        console.log(query)

        const x = await query.first();

    console.log(x)

        x.set("votes", 1);

        x.save();

    }

Okay like this it does work, but all the values are being saved as “null”

if you use console.log(x) after this line, the values are still “null”?

found the error already wow this took a long time

well sometimes it works, sometimes it shows undefined :rofl:

1 Like