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

model.refresh is not working properly | bug #42

Closed
seeden opened this issue Nov 2, 2016 · 7 comments · Fixed by #43
Closed

model.refresh is not working properly | bug #42

seeden opened this issue Nov 2, 2016 · 7 comments · Fixed by #43

Comments

@seeden
Copy link

seeden commented Nov 2, 2016

When I call model.refresh() I will get next exception:

SyntaxError: Unexpected token o in JSON at position 1
    at Object.parse (native)
    at constructor.jsonColumns.forEach.column (/Users/seeden/Documents/git/goodmodule.com/node_modules/bookshelf-json-columns/dist/index.js:38:38)
    at Array.forEach (native)
    at Quiz.parse (/Users/seeden/Documents/git/goodmodule.com/node_modules/bookshelf-json-columns/dist/index.js:36:32)
    at /Users/seeden/Documents/git/goodmodule.com/node_modules/bookshelf/lib/base/events.js:176:25
From previous event:
    at Quiz.triggerThen (/Users/seeden/Documents/git/goodmodule.com/node_modules/bookshelf/lib/base/events.js:175:32)
    at Quiz.<anonymous> (/Users/seeden/Documents/git/goodmodule.com/node_modules/bookshelf/lib/model.js:702:19)
    at tryOnImmediate (timers.js:543:15)
    at processImmediate [as _immediateCallback] (timers.js:523:5)
From previous event:
    at Quiz.<anonymous> (/Users/seeden/Documents/git/goodmodule.com/node_modules/bookshelf/lib/model.js:685:8)
From previous event:
    at Quiz.refresh (/Users/seeden/Documents/git/goodmodule.com/node_modules/bookshelf/lib/model.js:567:17)

Instead of this everything else working fine.

bookshelf-json-columns@2.0.0 with sqllite

@ricardogama
Copy link
Collaborator

ricardogama commented Nov 3, 2016

It seems the value is already parsed when the plugin fetched event is fired, so I suspect that is being done somewhere else. Do you have any custom events on you model or are you extending initialize or save methods?

It can also happens that the value comes already parsed from the fetch, which is unlikely, but I wonder if the model data you are refreshing was inserted with the plugin or with a raw query for example.

@seeden
Copy link
Author

seeden commented Nov 4, 2016

I am not extending initialize or save method

@ricardogama
Copy link
Collaborator

I can't reproduce this error, the only thing that occurs to me is that the fetched value is not a valid stringified JSON object. Can debug what value is being parsed when the error occurs?

Also, a snippet of your model and the code you're executing would help, along with the bookshelf and sqlite3 versions you're using.

@seeden
Copy link
Author

seeden commented Nov 9, 2016

This issue is happing when I use increment. Here is simple example

const model = await Model.where({id}).fetch();
await model.query().increment('likes', 1);
await model.refresh();

Your package is using parsed data.

@seeden
Copy link
Author

seeden commented Nov 9, 2016

I am using temp fix for this but it is not ideal because it can fail for pure string values

this.attributes[column] = typeof this.attributes[column] === 'string'
   ? JSON.parse(this.attributes[column])
   : this.attributes[column];

@ricardogama
Copy link
Collaborator

Ok I got the bug, it happens when querying updates or inserts through knex, which does not trigger the saving event and only triggers fetching.

I'll try to fix until the end of the week, thanks for the info!

@ricardogama
Copy link
Collaborator

Released as 2.0.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants