question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Parse method is not applied after save

See original GitHub issue

Hey everyone,

I’m not sure if the following behaviour is a bug or if it’s wanted. Help appreciated 😃

I’m using the bookshelf-camelcase plugin that camelcases column names when working with a model and transforms them back to snake_case when saving to the database. It uses the format and parse methods.

Everything works as expected when I fetch a model:

new User({id: 3}).fetch().then(function(user) {
  console.log(user);
});

results in

{
  "id": 3,
  "name": "John",
  "createdAt": "2015-12-31T14:25:48.000Z",
  "updatedAt": "2015-12-31T14:50:28.000Z"
}

However, when I create a new object and save it:

new User({name: "Ann"}).save().then(function(user) {
  console.log(user);
});

user looks as follows:

{
  "id": 4,
  "name": "Ann",
  "created_at": "2015-12-31T14:25:48.000Z",
  "updated_at": "2015-12-31T14:50:28.000Z"
}

Notice the snake case of created_at and updated_at.

The documentation states that the format method is applied after a fetch. So this behaviour might be wanted. If so, why? And what would be the best way to deal with it?

Thanks a lot.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
ricardogracacommented, Apr 28, 2016

@fredrikolovsson Well spotted. Parse and format really are confusing (#668).

I think better than an option would be for Bookshelf to just do the right thing. I think it’s safe to say that after a save all the attributes should be parsed again. Either that or fix the corner case of timestamps not being parsed at all after save if they’re not part of the attributes to save. @rhys-vdw What do you think?

1reaction
fredrikolovssoncommented, Apr 28, 2016

@cauboy & @ricardograca, I believe format should be replaced with parse in the issue name and the first two comments (the documentation link in the first comment correctly points to parse) 😄

As an alternative to doing refresh() or overriding save(), I tried parsing the response attributes, which worked for me. Going back to the original example it would be like this instead:

new User({name: "Ann"}).save().then(function(user) {
  console.log(user.parse(user.attributes));
});

@ricardograca do you think it would make sense to add [parse=false] bool as an option to save()? If yes, I could try to make a PR for it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Save success not working with parse.com object
I was having a similar problem and I found that making my function (e.g. $("#f1").submit(function(event) { return false fixed it. – lewis.
Read more >
SyntaxError: JSON.parse: bad parsing - JavaScript | MDN
JSON.parse() parses a string as JSON. This string has to be valid JSON and will throw this error if incorrect syntax was encountered....
Read more >
ParseObject - Documentation - Parse Server
Atomically decrements the value of the given attribute the next time the object is saved. If no amount is specified, 1 is used...
Read more >
Troubleshooting Common Issues with Parse
Avoid ever calling save on multiple ParseObject instances in quick succession. Local Datastore and Public Read Access. It looks like public read access...
Read more >
DateTime.Parse Method (System) - Microsoft Learn
If the input string represents a leap day in a leap year in the calendar used by the parsing method (see Parsing and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found