Parse method is not applied after save
See original GitHub issueHey 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:
- Created 8 years ago
- Comments:13 (7 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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?
@cauboy & @ricardograca, I believe
format
should be replaced withparse
in the issue name and the first two comments (the documentation link in the first comment correctly points toparse
) 😄As an alternative to doing
refresh()
or overridingsave()
, I tried parsing the response attributes, which worked for me. Going back to the original example it would be like this instead:@ricardograca do you think it would make sense to add
[parse=false] bool
as an option tosave()
? If yes, I could try to make a PR for it.