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

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

Open
ADrejta opened this issue Sep 8, 2020 · 3 comments
Open

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

ADrejta opened this issue Sep 8, 2020 · 3 comments
Labels

Comments

@ADrejta
Copy link

ADrejta commented Sep 8, 2020

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.

@ricardograca
Copy link
Member

ricardograca commented Sep 8, 2020

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 }
      )

@ricardograca ricardograca added this to To do in Version 1.x.x via automation Sep 8, 2020
@ADrejta
Copy link
Author

ADrejta commented Sep 11, 2020

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.

@chuksgpfr
Copy link

I think forge has been depreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Version 1.x.x
  
To do
Development

No branches or pull requests

3 participants