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

isn’t what include is supposed to do?

I think that you have to save it first, do you see it in your dashboard in that Posts table?

so you need to do a post.set(“comments”,comment)?

yes, something like that

but the problem is that the post query already exists, you know? So doing a Moralis.query and saving it to that gives me an error Posts it is not a function

const createNewComment = async (e, content) => {

        e.preventDefault()

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

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

        query.equalTo("title", resultId);

        const newComment = Moralis.Object.extend("Comments");

        const comment = new newComment();

        comment.set("content", content);

        comment.set("author", Moralis.User.current());

        comment.save();

        Posts.set("Comments", comment);

        Posts.save();

        query.include("Comments");

      }

it doesn’t work like that, you have to create an object, like you did with Comments:

or to update an existing object.

but if the object already exists, do I still need to create another one or how would you update ana existing one?

to update an existing one, you run a query to get the object, and after that you can update it.

yes but for example if i try to update this:

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

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

        console.log(query)

        query.set("id", "hello");

        query.save();

it gives me error query.set is not a function

I wouldn’t expect to work what is there, you want to use .set on a query? you have to get an object with .find() or .first() before accessing that object

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);

        query.set("votes", votes++)

       

        query.save()

    }

Okay so would this be correct? I fetch the query and then update it with votes. Why it gives me error can’t assign to constant variable

Before trying to set anything, you have to run the query and get the result. Usually you run a query with query.find() or query.first()

so this should work?

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);

        query.find()

        console.log(query)

        query.set("votes", votes++)

        query.save()

    }

Write there x = await query.find()
And then console.log(JSON.stringify(x))
So that you can see what you have there
You should get a list on success, you have to get an element from that list and the you can use .set on that particular object/element

Okay this gets me the specific array that’s been clicked and it sets the votes to sum up one.

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)

        finalResults.set("votes", votes++)

        finalResults.save()

    }

But why does it still give me function error

You should look at that data, to understand what is there, I don’t think that you looked in that array yet to identify the object.

you mean console.log(finalResults)? It does give me the array corresponding to the button i click

Yes, that. After that you can not use .set on an array, you have to get an individual element from that array and use .set on that element.

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

like this?

Something like that, but you also have to call .save after that