Skip to content

Error: constructor undefined when using with (3rd party library) object #53

@EliasBeyer

Description

@EliasBeyer

I tried to use localStorage to store an object from another library and get this error:

TypeError: e2.constructor is undefined
    a index.mjs:1
    a index.mjs:1
    a index.mjs:1
    A index.mjs:1
    setValue index.mjs:1
    x index.mjs:1
    subscribe index.mjs:1
    x index.mjs:1
    X index.mjs:1
    H index.mjs:1
    <anonymous> store.js:19
app.js:16:41

Steps to reproduce:

  • npm create svelte@latest my_test (doesn't matter the configuration, tested with skeleton app, without types and with typescript, no linter etc)
  • install this library and another external one: npm install @macfja/svelte-persistent-store and npm install obs-websocket-js
  • create src/routes/store.js:
import { localWritable } from "@macfja/svelte-persistent-store"
import { writable } from "svelte/store"
import OBSWebSocket from 'obs-websocket-js';

export class BaseMyObj {}
class MyObj extends BaseMyObj {}

// export const obs = writable( new OBSWebSocket() ) // works
export const myobj = localWritable( "myobj" , new MyObj() ) // works
export const obs = localWritable( "obs" , new OBSWebSocket() ) // error
  • use store.js in +page.svelte: import { myobj } from "./store"

I am fairly new to webdev/js/svelte so it might be an error on my side.

What I have tried so far:

  • changed the sveltekit configuration (JS/TS) with the same error
  • tried to dig into the error itself, but there is no e2 or e2.constructor anywhere in the minified JS in the browser
  • tried to recreate the error with a custom class (that extends a base class) in JS/TS, these work just fine
  • also used the base functions (persist and all the available alias for localstorage) with the same error

I guess this error may be specific for the combination of those two libraries, however I lack the insight of both libraries to get into it deeper, especially with other 3rd party packages

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingquestionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions