techfort / LokiJS

javascript embeddable / in-memory database
http:/techfort.github.io/LokiJS
MIT License
6.73k stars 482 forks source link

Error in eqJoin map function when returning object with $loki #850

Closed jmhmd closed 3 years ago

jmhmd commented 4 years ago

I am trying out the eqJoin function for the first time, and was running into an error when trying to combine the left and right objects in the mapper function.

When I tried to use object spread syntax to add a field to left from right:

const stagedJobs = db.jobs
    .chain()
    .find({
      status: { $in: ['staged', 'error', 'processing'] },
    })
    .eqJoin(db.users, 'userId', '$loki', (left, right) => {
       return { ...left, username: right.username }; 👈 
    })
    .data();

I get the error:

Error: Document is already in collection, please use update()
    at Collection.add (/Users/jasonhostetter/dev/pacsbin-project/rigel/node_modules/lokijs/src/lokijs.js:5951:15)
    at Collection.insertOne (/Users/jasonhostetter/dev/pacsbin-project/rigel/node_modules/lokijs/src/lokijs.js:5738:17)
    at Collection.insert (/Users/jasonhostetter/dev/pacsbin-project/rigel/node_modules/lokijs/src/lokijs.js:5686:20)
    at Resultset.eqJoin (/Users/jasonhostetter/dev/pacsbin-project/rigel/node_modules/lokijs/src/lokijs.js:3961:23)

I figured out that it depends on whether I am trying to pass $loki as a field in the returned object.

This gives an error:

.eqJoin(db.users, 'userId', '$loki', (left, right) => {
      return {
        $loki: left.$loki, 👈 
        username: right.username,
        description: left.description,
        studies: left.studies,
        status: left.status,
      };
    })

This works as expected:

.eqJoin(db.users, 'userId', '$loki', (left, right) => {
      return {
        jobId: left.$loki, 👈 
        username: right.username,
        description: left.description,
        studies: left.studies,
        status: left.status,
      };
    })

Is there a way to just add a field or fields to the left object of the join and return it?

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.