Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update with array as value adds number objects #1195

Closed
Christilut opened this issue Dec 15, 2020 · 3 comments · Fixed by #1197
Closed

Update with array as value adds number objects #1195

Christilut opened this issue Dec 15, 2020 · 3 comments · Fixed by #1197
Labels

Comments

@Christilut
Copy link

I might be completely missing something but this is odd behavior to me:

const field = 'authors'

  await db.books.update(id, {
     [field]: [{ foo: 'bar' }, { foo2: 'bar2'} ]
   })

This ends up with authors.0 and authors.1 but it should just be an array.
What's going on here?

@dfahlander
Copy link
Collaborator

Tried to reproduce in https://jsitor.com/DGNaFfv5S but it seems to behave as it should.

const db = new Dexie("bubbu");
db.version(1).stores({
  books: 'id'
});

Promise.resolve().then(async ()=>{
  await db.books.put({id: 1});
  console.log (JSON.stringify(await db.books.get(1)));
  const field = 'authors'
  await db.books.update(1, {
    [field]: [{ foo: 'bar' }, { foo2: 'bar2' }]
  });
  console.log(JSON.stringify(await db.books.get(1)));
}).catch(console.error);

Output:

{"id":1}
{"id":1,"authors":[{"foo":"bar"},{"foo2":"bar2"}]}

@Christilut
Copy link
Author

Thanks for confirming that's not it. I figured out it came from the updating hook: https://jsitor.com/32Nht9kOY

The modifications are saved to the cache as authors.0 etc. It is saved to the database like normal arrays so a cache clear fixes it.
Is this normal in the modifications object though?

@dfahlander
Copy link
Collaborator

Ok! The array is somehow interpreted as an object. I will investigate this. There was a similar issue and PR recently #1130. I also notice that the behavior between using dexie 2.x and 3.x.

@dfahlander dfahlander added the bug label Dec 16, 2020
dfahlander added a commit that referenced this issue Dec 16, 2020
dfahlander added a commit that referenced this issue Dec 16, 2020
…nd never go into them to treat array indices as if they were object props.

Resolves #1195.
dfahlander added a commit that referenced this issue Dec 16, 2020
dfahlander added a commit that referenced this issue Dec 16, 2020
…nd never go into them to treat array indices as if they were object props.

Resolves #1195.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants