KayelGee / token-attacher

MIT License
11 stars 18 forks source link

Errors after deleting attached before unattaching #60

Closed dmrickey closed 2 years ago

dmrickey commented 2 years ago

I created a macro to attach a template to a token (basically an aura that follows the token)

const templates = await canvas.scene.createEmbeddedDocuments(
    "MeasuredTemplate",
    [{
        t: "circle",
        user: game.user.id,
        x: token.x + canvas.grid.size/2,
        y: token.y + canvas.grid.size/2,
        direction: 0,
        distance: 30,
        borderColor: "#FF0000",
        //fillColor: "#FF3366",
    }]);

tokenAttacher.attachElementToToken(templates[0], token, suppresNotification=true);

I move the token around and that's fine, it works as expected.

I go to the Measurement Controls and delete the template (note I did not unattach it first via TA). Now every time I move the token I get this error.

token-attacher.js:576 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'data')
[Detected 3 packages: token-attacher, lib-wrapper, drag-ruler]
    at token-attacher.js:576
    at Array.map (<anonymous>)
    at Function._UpdateAttachedOfBase (token-attacher.js:576)
    at Function.UpdateAttachedOfToken (token-attacher.js:556)
    at token-attacher.js:213
    at Function._call (eval at <anonymous> (listeners.js:91), <anonymous>:4:14)
    at Function.callAll (foundry.js:153)
    at ClientDatabaseBackend.callback (foundry.js:8976)
    at foundry.js:8946
    at Array.map (<anonymous>)
    at ClientDatabaseBackend._handleUpdateEmbeddedDocuments (foundry.js:8946)
    at ClientDatabaseBackend._updateEmbeddedDocuments (foundry.js:8835)
    at async Function.updateDocuments (document.mjs:366)
    at async DragRulerRuler.animateEntities (foundry_imports.js:90)
    at async DragRulerRuler.moveEntities (foundry_imports.js:41)

If I open the TA menu and hit Delete all elements then I get this error.

collection.mjs:106 Uncaught (in promise) Error: The key zruPSxurVImmZTQx does not exist in the EmbeddedCollection Collection
[No packages detected]
    at Map.get (collection.mjs:106)
    at ClientDatabaseBackend._preUpdateDocumentArray (foundry.js:8851)
    at ClientDatabaseBackend._updateEmbeddedDocuments (foundry.js:8824)
    at ClientDatabaseBackend.update (backend.mjs:153)
    at async Function.updateDocuments (document.mjs:366)

I had some logging in the macro, and can verify that the key it's looking for when I hit delete all is the id of the template that was created.

p.s. This is probably reproducible by manually creating, attaching, and deleting a template, but I didn't go through those steps to verify

dmrickey commented 2 years ago

P.s. I'm using the PF1 system in case that matters

KayelGee commented 2 years ago

When you deleted something that is attached it should in theory detach it before deletion. I'll have to check the code if there's a bug in there.

KayelGee commented 2 years ago

So the error comes from me updating the deleted elements and then updating the base token. Because the elements are already deleted the update throws an exception and the base token update never happens. Will be fixed in next update.

dmrickey commented 2 years ago

Sweet. Glad you were able to get it sorted out