neumino / thinky

JavaScript ORM for RethinkDB
http://justonepixel.com/thinky/
Other
1.12k stars 128 forks source link

Errors with addRelation #593

Closed vmarcel closed 7 years ago

vmarcel commented 7 years ago

I am having a strange error when I use addRelation on a new document.

    var projectId = req.path.split('/').slice(-1)[0]; // get the id from url path

    Project.get(projectId).update({volcan_lead: req.user.id, volcan_name: req.user.alias, started: r.now()}).run().then(function(projDoc) { // get project and update it 
        volcanProject.filter({volcan_id: req.user.id}).addRelation("projects", {id: projDoc.id});

        volcanProject.filter({volcan_id: req.user.id}).getJoin({projects: true}).run().then(function(volcanProj) {
            console.log(volcanProj);

            // console.log(projDoc);
        });

The full stack trace is:

{ changes: [],
  deleted: 0,
  errors: 1,
  first_error: 'Expected type DATUM but found SEQUENCE:\nVALUE SEQUENCE',
  inserted: 0,
  replaced: 0,
  skipped: 0,
  unchanged: 0 }
[ model {
    id: 'dce07846-f7fd-44c9-9896-099ad6a438e7',
    projects: [ [Object] ],
    volcan_id: 'e37e6ef4-b2f9-41ee-bac0-a78aec0135f0' } ]
Unhandled rejection InvalidWriteError: An error occured during the write
    at Query._validateQueryResult (/Users/vlad/code/volcan_dev/node_modules/thinky/lib/query.js:211:27)
    at /Users/vlad/code/volcan_dev/node_modules/thinky/lib/query.js:166:19
    at tryCatcher (/Users/vlad/code/volcan_dev/node_modules/rethinkdbdash/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/Users/vlad/code/volcan_dev/node_modules/rethinkdbdash/node_modules/bluebird/js/release/promise.js:504:31)
    at Promise._settlePromise (/Users/vlad/code/volcan_dev/node_modules/rethinkdbdash/node_modules/bluebird/js/release/promise.js:561:18)
    at Promise._settlePromise0 (/Users/vlad/code/volcan_dev/node_modules/rethinkdbdash/node_modules/bluebird/js/release/promise.js:606:10)
    at Promise._settlePromises (/Users/vlad/code/volcan_dev/node_modules/rethinkdbdash/node_modules/bluebird/js/release/promise.js:685:18)
    at Async._drainQueue (/Users/vlad/code/volcan_dev/node_modules/rethinkdbdash/node_modules/bluebird/js/release/async.js:138:16)
    at Async._drainQueues (/Users/vlad/code/volcan_dev/node_modules/rethinkdbdash/node_modules/bluebird/js/release/async.js:148:10)
    at Immediate.Async.drainQueues [as _onImmediate] (/Users/vlad/code/volcan_dev/node_modules/rethinkdbdash/node_modules/bluebird/js/release/async.js:17:14)
    at tryOnImmediate (timers.js:543:15)
    at processImmediate [as _immediateCallback] (timers.js:523:5)

I am not sure what to do. Both docs are already created, I am unsure what is happening. Any help would be appreciated

neumino commented 7 years ago

Sorry for the late answer, I missed this issue.

filter returns a sequence, while addRelation expect a single document.

You probably want something like this:

volcanProject.filter({volcan_id: req.user.id}).nth(0).addRelation(...)