question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Possible bug: Duplicate cache of same doc if updated inside Collection.after.insert

See original GitHub issue

Hi,

I’m using the matb33:collection-hookspackage.

It provides hooks like Collection.after.insert.

Suppose I have Questions collection denormalized into Lectures collection.

When I create a question “What’s up?”, the expected behaviour is for Lecture document to look like this:

// Lecture Document
_id: ....
questionsCache: [
  {
    _id: 5e44dRMouE9BiwgLP,
   body: "What's up?"
  }
]

However, suppose I have a after.insert hook in which I update that question, like this:

Questions.after.insert((userId, doc) => {
  Questions.update({_id: doc._id}, {$set: {someFlag: true}});
})

When this is the case, this is what questionsCache looks like instead:

// Lecture Document
_id: ....
questionsCache: [
  {
    _id: 5e44dRMouE9BiwgLP,
   body: "What's up?"
  },
  {
    _id: 5e44dRMouE9BiwgLP,
   body: "What's up?",
   someFlag: true
  },
]

Is there perhaps a race condition going on, because the after.insert hook probably fires before the cache is registered?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Hertebycommented, Dec 3, 2017

Hi! Grapher uses my denormalization package, which is actually built on Collection Hooks too. The order in which Collection Hooks run is determined by the order they were set up. You could try swapping the order you create the denormalization and set up your insert hook, or you could use my cacheField function, which is tested to work properly with the other cache types.

0reactions
nadeemjacommented, Dec 4, 2017

Cool. I’ll try 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Customizing the behavior of cached fields - Apollo GraphQL
An array of key arguments that help the cache avoid storing unnecessary duplicate data. You provide field policies to the constructor of InMemoryCache...
Read more >
How the Document Cache Shape Works - Boomi Community
The doc cache is used in two distinct ways in the platform: Storing documents to look up information inside of the document based...
Read more >
Chrome is incorrectly storing and sending duplicate cookies ...
(4) Another cookie update is received. This time, the update will cause us to insert a duplicate into the cookie database. This happens...
Read more >
c# - Collection was modified; enumeration operation may not ...
Why this error? ... In general .Net collections do not support being enumerated and modified at the same time. If you try to...
Read more >
db.collection.insert() — MongoDB Manual
If the document contains an _id field, the _id value must be unique within the collection to avoid duplicate key error. Transactions. db.collection.insert()...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found