Bookshelf: Can't update with new Model(), but can update with Model.forge({})

Created on 8 Sep 2020  Â·  3Comments  Â·  Source: bookshelf/bookshelf

Introduction

When trying to update a model i am getting a EmptyResponse error

Bookshelf version: 1.2.0
Knex version: 0.21.4

Steps to reproduce issue

When i want to save a model using:

   await new Exam().where({ id: examId, professor_id: userId }).save(
        {
          attribute1: value1,
          attribute2: value2
        },
        { patch: true }
      );

I'm getting a EmptyResponse error:

'CustomError: EmptyResponse  
at Child.<anonymous> (/home/aurel/projects/omnescode/node_modules/bookshelf/lib/model.js:719:21)
at PassThroughHandlerContext.finallyHandler (/home/aurel/projects/omnescode/node_modules/bluebird/js/release/finally.js:57:23)   
at PassThroughHandlerContext.tryCatcher (/home/aurel/projects/omnescode/node_modules/bluebird/js/release/util.js:16:23)   
at Promise._settlePromiseFromHandler (/home/aurel/projects/omnescode/node_modules/bluebird/js/release/promise.js…8)
at  _drainQueueStep (/home/aurel/projects/omnescode/node_modules/bluebird/js/release/async.js:93:12)    
at _drainQueue (/home/aurel/projects/omnescode/node_modules/bluebird/js/release/async.js:86:9)   
at Async._drainQueues (/home/aurel/projects/omnescode/node_modules/bluebird/js/release/async.js:102:5)   
at Immediate.Async.drainQueues [as _onImmediate] (/home/aurel/projects/omnescode/node_modules/bluebird/js/release/async.js:15:14)    
at processImmediate (internal/timers.js:439:21)'

While when doing it like this:

   await Exam.forge({ id: examId, professor_id: userId }).save(
        {
          attribute1: value1,
          attribute2: value2
        },
        { patch: true }
      );

I'm actually getting the model to be updated with the values that i give to it.

I read the docs and it said that if i pass a {require: false} the error should not show, however the problem still persists even though i am passing the require:false and the actual model that i want to update does exist in the database.

bug

Most helpful comment

Thanks for the quick reply!

Yes the method you proposed does work.

I do understand that this bug is not really something critical since there seem to be other working methods, but i thought giving the bookshelf team a heads up on this issue was a good idea.

All 3 comments

I think I know why this happens. In the first case the model instance created by new Exam() is empty, and when the model tries to refresh itself after saving there is no id to look for so you get the EmptyResponse error. Not sure how to solve this one, but it should be possible.

This should also work:

await new Exam({ id: examId }).where({ professor_id: userId }).save(
        {
          attribute1: value1,
          attribute2: value2
        },
        { patch: true }
      )

Thanks for the quick reply!

Yes the method you proposed does work.

I do understand that this bug is not really something critical since there seem to be other working methods, but i thought giving the bookshelf team a heads up on this issue was a good idea.

I think forge has been depreciated.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

demisx picture demisx  Â·  4Comments

Wtower picture Wtower  Â·  4Comments

Playrom picture Playrom  Â·  4Comments

jesobreira picture jesobreira  Â·  3Comments

leebenson picture leebenson  Â·  4Comments