#Issues with object assignment in quick.db

26 messages · Page 1 of 1 (latest)

barren geyser
#

Im using default quick.db, no addons or anything. For some reason I can grab the field

{
  xp: 112,
  cooldowns: {
    daily_cooldown: 1684372047605,
    weekly_cooldown: 1684887595281,
    rep_cooldown: 0
  },
  coins: 1064,
  job: {},
  bank: 0,
  rep: 0,
  ingame: false,
  badges: [ 'alpha' ]
}```

Im trying to set "job" to ```js
{ pay: 200, level: 5, benefits: [], benefits_str: 'No benefits.' }```

But it isnt setting. For some reason when I grab it again it turns back to {}

```js
await handler.database.subtract(`users.${interaction.user.id}.coins`, 50);
            console.log(await handler.database.set(`users.${interaction.user.id}.job`, job_obj));```

Heres the code ive been using to set it, my handler has a fillTemplate function on the grab
#

I don't think its this as the cooldowns work fine with it

#
function fillTemplate(input, template) {
  // Remove any fields in the input object that are not in the template
  for (let key in input) {
    if (!template.hasOwnProperty(key)) {
      delete input[key];
    } else if (
      typeof template[key] === 'object' &&
      template[key] !== null &&
      !Array.isArray(template[key])
    ) {
      // Recursively remove fields in nested objects
      input[key] = fillTemplate(input[key] || {}, template[key]);
    }
  }

  // Add any missing fields from the template to the input object
  for (let key in template) {
    if (
      typeof template[key] === 'object' &&
      template[key] !== null &&
      !Array.isArray(template[key])
    ) {
      // Recursively fill nested objects
      input[key] = fillTemplate(input[key] || {}, template[key]);
    } else if (!input.hasOwnProperty(key)) {
      input[key] = template[key];
    }
  }

  // Return the updated input object
  return input;
}```
upper shuttle
#

that is quite important to know to find out what going wrong

barren geyser
#
const myMemberDB = await handler.getUserDB(interaction.user.id);

>handler.getUserDB

async getUserDB(id) {
    const db = await this.database.fetch(`users.${id}`) || {};
    const filled = fillTemplate(db, this.templates.user);
    return filled;
  }
#

It is working, js { xp: 117, cooldowns: { daily_cooldown: 1684372047605, weekly_cooldown: 1684887595281, rep_cooldown: 0 }, coins: 914, job: {}, bank: 0, rep: 0, ingame: false, badges: [ 'alpha' ] } I have grabbed it

#

If anything, It would be the fillTemplate, but it doesn't overwrite cooldowns which is weird

upper shuttle
#

first of, there seems to be a really weird confusion right now.
fetch doesn't exist anymore in version v9
and you use it.
you also use await which is only in v9 that it's needed ...

#

so which version of quick.db are you using?

barren geyser
#

oh mb, Im using a compatability layer, basically just changes the name of functions because I cant fix all my code for updates most times.

fetch is table.get()
set is table.set()
fetchall is table.all()
has is table.has()
add push and sub are the same too

#
table = db.table(config.name);```
#

there

upper shuttle
#

let me just say that using users.${id} as a key is extremely inefficient

barren geyser
#

What would be better for that?

upper shuttle
#

not a dot. like a underscore users_${id} because with a dot. it makes an object. which means it's in the same row in the database. so right now you have a gigantic big json in a single row in the database if you have a lot of users. there is a text limit to sqlite rows

barren geyser
#

okay ill change that to an _

#

is there a way I can fix that jobs thing though?

upper shuttle
#

From what I can see, there is no problem.
Have you tried just setting it and getting it back to see the value? or did you passed it in fillTemplate when you checked ?

#

from how it is used with quick.db, there seems to be no problem. so I want to make sure it's not something you made in between that causes the issue

barren geyser
#

Okay yeah ill check. I may be awhile as im getting ready for work. Thank you for the help ill lyk what happens

upper shuttle
#

No worries, I am here to help, take your time. mention me if you want quick replies when you are back

barren geyser
barren geyser
#

I fixed it, turns out the template needs the blank variables for recursive objects

#
"user":{
        "coins": 0,
        "bank": 0,
        "rep": 0,
        "job": { "display_name":"Unemployed", "pay":0, "level":0, "benefits":[],"benefits_str":"No benefits." },
        "ingame": false,
        "xp": 0,
        "badges":["alpha"],
        "cooldowns":{
          "daily_cooldown":0,
          "weekly_cooldown":0,
          "rep_cooldown":0
        }
},```