#Hi all. I have an error I'm not sure

1 messages · Page 1 of 1 (latest)

dusky lark
#

The model in question is:

import { FFItemDataModel } from './baseItem';

const {
  HTMLField, SchemaField, NumberField, StringField, FilePathField, ArrayField, BooleanField
} = foundry.data.fields;

export class JobModel extends FFItemDataModel {
  static defineSchema() {
    return {
      ...super.defineSchema(),  // Merge with the base model schema
      grants: new SchemaField({
        list: new ArrayField(
          new StringField({ required: true, initial: '' })
        ),
        value: new BooleanField({ required: true, initial: false })
      }),
    }
  }
}
#

The update that's causing the bother looks like this:

export async function deleteItemLink($doc, key, index) {
  const itemList = [...$doc.system?.[key]?.list];
  const spliced = itemList.splice(index, 1);
  await $doc.update([`system.${key}.list`], spliced);
  return spliced;
}
#

I'm confused by the validation error sort: must be an integer I'm not aware of having a sort column in my schema. So what does it mean?

kind island
dusky lark
kind island
dusky lark
#

I mean the update happens and looks correct in the data despite the validation errors

kind island
dusky lark
#

My understanding was that it could also accept a string using dot notation as first argument and a value as second argument. At least I've been using it that way without trouble.

kind island
#

You may want this instead

.update({[`system.${key}.list`]: itemList})
dusky lark
#

Sure I switched it out to :

  await $doc.update({system: { [key]: { value: 1, list: itemList } }});

But same validation error

kind island
dusky lark
#

I lied. The validation error disappears if I use the object syntax instead of the string dot syntax, although the update happens either way.

kind island
#

Peculiar

#

I'll have to take a look at the source code and see what's up

dusky lark
#

So I have two functions, one to add an item to the list and one to delete. Both were using the dot notation syntax. I was adding and deleting successfully except for the validation error.

Then I switched the delete to use the object syntax and that removed the validation error when deleting.

When I tried the same thing for the add function, it stopped working... so it doesn't update correctly using the object syntax.

i.e. this works:

await $doc.update([`system.${key}`], { value: 1, list: itemList } );

But for some reason, this does not:

await $doc.update({system: { [key]: { value: 1, list: itemList } }});

The latter does add an item without validation errors but the array item value is [object object] instead of a {uuid: "Item.ycgTsS5CrSfLgZ1O"}

kind island
#

Uh, that's weird

dusty widget
#

Ah I see up in the chat

dusky lark
#

Something else I just noticed is that although the update is updating the front-end, it's not persisting after a refresh

dusty widget
#

Actually I just noticed

#

Can you change the parameter from $doc to doc

#

For the function

#

Since the dollar sign is the actual store accessor

#

It may be confusing things

dusky lark
#

If I do that I get doc.update is not a function

dusty widget
#

I meant ONLY the parameter

dusky lark
#

huh?

dusty widget
#

Keep the dollar sign by the update function to actually access the document

dusty widget
#

But I guess it doesn't change much if it already works in the other cases

dusky lark
#

$doc is just poorly named parameter. It's the output of the store, so it's just the document.

#

That's not entirely true, thinking about it but I think it's a red herring. $doc.update is just the foundry's update method

kind island
dusky lark
#

I wouldn't even know how to do that 🙂

#

If I log $doc, it's type is Item

kind island
dusky lark
#

And it's key / values look like what you'd expect from an Item document

dusty widget
#

I will note I see the model being a boolean while you pass an integer but the sort thing is still befuddling me

dusky lark
dusky lark
kind island
dusty widget
#

This is UI code, not the document code

#

Nor the actual app code, but the component of which the document is passed down to

kind island
dusty widget
#

Sure just this is a completely different thing in this case.

dusky lark
#

I haven't neither extended or overridden the Item class itself, so it is the default Foundry implementation.

kind island
#

Ok, I'm just confused about how what you're describing is happening (especially the idea of passing a string+array as update args; looking at the method signature for Document.update I can't see how that could ever work.

Something odd is going on, based on what you describe (an update to a document should persist the data to the database, that's the whole point of it). My gut instinct is to blame the Svelte stuff somehow, but that is mostly because I have zero familiarity with it and don't know how it works like I do Foundry's side of things, lol

#

It feels like there's another layer of processing going on somewhere, somehow, that is mutating things before issuing the update, especially with what you're talking about regarding the data not being saved as-expected and the fact that the data you're talking about saving doesn't seem to line up with the schema of the DataModel you mentioned earlier

dusty widget
dusky lark
#

I’m AFK for an hour or so

dusky lark
#

It’s likely to be pebcak because these techniques are working in other modules / systems I’ve built on Svelte. But it certainly has me scratching my head.

dusky lark
#

ok so I built a standard hbs item sheet to do some testing.

The sort issue is caused by me incorrectly using the dot notation update syntax. I got confused.

This doesn't work:

await $doc.update([`system.${key}`], { value: 1, list: itemList } );

This does work:

await $doc.update({[`system.${key}`]: { value: 1, list: itemList }} );

That's all it was 🙂

#

I still don't know why the object syntax isn't working in my Svelte example though... will try work backwards now and see if I can figure that out.

dusky lark
#

Ok @kind island I've taken Svelte completely out of the picture and used the object syntax. It's still storing ['object Object'] instead of the object:

export default class ItemSheetStandard extends ItemSheet {
  constructor(...args) {
    super(...args);
  }
  get template() {
    return `systems/foundryvtt-final-fantasy/assets/templates/${this.item.type}.hbs`;
  }
  activateListeners(html) {
   super.activateListeners(html);
    html.find('[data-action="testButtonClick"]').click(this._onTestButtonClick.bind(this));
    const dropZone = html.find('[data-action="drop-handler"]');
    dropZone.on('dragover', this._onDragOver.bind(this));
    dropZone.on('drop', this._onDrop.bind(this));
  }
  _onTestButtonClick(event) {
    event.preventDefault();
    console.log("Test button clicked");
  }
  _onDragOver(event) {
    event.preventDefault(); 
    const dropZone = event.currentTarget;
    dropZone.classList.add('drag-over'); dropZone.addEventListener('dragleave', () => {
      dropZone.classList.remove('drag-over');
    });
  }
  async _onDrop(event) {
    event.preventDefault();
    const originalEvent = event.originalEvent || event; 
    const data = JSON.parse(originalEvent.dataTransfer.getData("text/plain"));
    const item = await Item.implementation.fromDropData(data);
    const list = []
    list.push({ uuid: item.uuid })
    const key = 'grants'
    await this.item.update({system : {[key] : { list }}});
    game.system.log.d('item', this.item.system.grants.list) //- outputs `0: {['object Object']}
  }
}
#

Is there something in my schema for that signature that's wrong? (See earlier post for the model)

#

I think it's probably that as I'm specifying that the ArrayField should contain a string, so it's converting the object to a string 😕

dusty widget
#

That sounds about right

#
grants: new SchemaField({
        list: new ArrayField(
          new StringField({ required: true, initial: '' })
        ),
        value: new BooleanField({ required: true, initial: false })
      }),
kind island
dusky lark
#

Yeah that was it. Ok. So a multi-layered flopped pebcak souflé... my specialty! 😅

kind island
#

You hit the drawback of data models. When you define a DataModel, you're locked into rigidly following that exact structure

dusty widget
#

Gotta type it 🤭

#

"You're pushing an object into string[] sire"

dusky lark
#

Yeah, I'm finding the models significantly more difficult for me than ye olde template.json was. I find I'm going slower.

kind island
#

I mean, loosely typed languages are easy, you've just gotta not screw up 🤷‍♂️

dusky lark
#

But I'll probably get used to it