SOLVED: Database Query add new Column issue

I have a database class named Rooms and it is sync’d to my contract events when a new room is created. Everything works fine except one thing: the new column I added to the class is defaulting to undefined despite me specifying a default value of 0.

So I have a room status number field column that I added to this table and I set the default value to 0 and I also made it a required field, however, it is defaulting to “undefined” and only when I manually double click on the fields in the dashboard panel do they update to the default value of 0 that I set when I added the column.

This is a problem because I am querying this column from my dapp and because it is set to undefined by default I do not get that result in my query at all.

You can see the images below: The first image is what my dashboard column looks like and the next 2 images show the return data of 2 objects; 1 object with a room status value of 0 and the other with a room status of undefined.

The undefined value object does not even return the column data at all.


delete 2
delete 3

Hi,

I had the impression that you can not set a default value in the new dashboard interface.

You can use beforeSave to set that room_status to 0 in case that it is undefined.

how does that query looks like that doesn’t returns the rows that have undefined for that column? (undefined in this case means that it is not set, being mongo db the data base)

Hey, cryptokid,

I had the impression that you can not set a default value in the new dashboard interface.

New dashboard? Are you referring to the new UI preview? I took a screenshot of what the options are when you add a new column below:

You can use beforeSave to set that room_status to 0 in case that it is undefined.

The problem is I have to first read the value to determine the next course of action to take. I was under the impression that when a new entry would get created into the table that this column I am referring to would default with a value of 0 instead of undefined. Keep in mind I am not manually creating entries to this table as these are synced events from my contract.

how does that query looks like that doesn’t returns the rows that have undefined for that column?

The query looks the exact same as a regular query except the object is missing the key-value pair for room_status when the value is undefined. Below you can see that the second image is missing the room_status field altogether.

regular returned query:
delete 2

undefined value returned query:
delete 3

how does that regular query looks like?

for the dashboard, it looks like it was a fix added, it was not like that all the time

in beforeSave you can modify the object that will get saved, and you can set that default value 0 directly there

the default value may be set only for the new events maybe?

how does that regular query looks like?

const { fetch: fetchQuery, data: queriedData, error: queriedError, } = useMoralisQuery( 'RoomsCreated',
    (query) => query.descending('roomId'), [], { autoFetch: false, } )

in beforeSave you can modify the object that will get saved, and you can set that default value 0 directly there

How can I get access to this beforeSave feature? I don’t see the option in the dashboard

that beforeSave is a hook that you can set in cloud code:

https://docs.moralis.io/moralis-dapp/cloud-code/triggers#beforesave

1 Like

Thanks this works perfectly. If I may ask another related question. If I want to now update that column value from my frontend how can I achieve that?

I have this code in my react app for updating that column value in the db:

const UpdateRoomStatus = async () => {

    const results = await fetchQuery()

    if (results != undefined) {
      for (let index = 0; index < results.length; index++) {
        if (results[index].attributes.roomId === roomID.toString()) {
           results[index].set("room_status", 1)
            return
        }
      }
    }
  }

It’s not saving the new value though. From my reading, it seems maybe I don’t have permission to save into the db? But in the db dashboard the class has public read+write status so why not?

I’m not familiar with react, you may have to use results[index].save()

1 Like

Unfortunately, this didn’t work. I think the reason is that the return object sent back from the query is not what I assumed. The results[index] object that gets sent back looks like this:

So results[index].set(“room_status”, 1) doesn’t make sense?

I tried results[index].attributes.set(“room_status”, 1) but I got an error saying that .set method doesn’t exist on attribute object

I don’t know if I can use the result of this query to set any new data on it on the backend.

It won’t work this way. You should do it as @cryptokid mentioned.
After results[index].set("room_status", 1), you can then await results[index].save()

You can get back to your db and refresh to verify if its updated properly

1 Like

Hey, this didn’t work for me. I think because “room_status” is an attribute of the class object that gets returned back from the query. Updating it like this seems like more for cloud code, or am I making the wrong assumption?

Edit: after some testing I tried this variation:

results[index].set("attributes", {room_status: 1})

It didn’t work as intended but it did add a new column and save it to the class. Really weird that it will make a new attributes column and save that data to it but it won’t update the original room_status.

Okay, I’ve solved this issue. Thanks to @cryptokid once again and @qudusayo for the advice to try again.

For other people in my situation: For some reason updating a number field in your class does not update. I instead created another column this time of type string and using the below successfully updates the field in the db.

results[index].set("room_status", "1")
await results[index].save()
1 Like