fetch doesn't return null if query returns no results
See original GitHub issueReturns a promise, which will resolve with the fetched Model, or null if the model isn’t fetched.
// Fetch a model based on the currently set attributes,
// returning a model to the callback, along with any options.
// Returns a deferred promise through the Bookshelf.sync.
// If `{require: true}` is set as an option, the fetch is considered
// a failure if the model comes up blank.
fetch: function(options) {
var model = this;
options || (options = {});
return this.sync(options)
.first()
.then(function(resp) {
if (resp && resp.length > 0) {
model.set(model.parse(resp[0], options), _.extend({silent: true}, options))._reset();
if (!options.withRelated) return resp;
return new EagerRelation(model, resp)
.fetch(options)
.then(function() { return resp; });
} else {
if (options.require) return when.reject(new Error('EmptyResponse'));
model.clear({silent: true})._reset();
return {};
}
})
.then(function(resp) {
model.trigger('fetched', model, resp, options);
console.log ('hello')
return model;
});
},
It returns model
with empty attributes.
I’m trying to write a findOrCreate
method, so returning null
would be handy.
Issue Analytics
- State:
- Created 10 years ago
- Reactions:1
- Comments:10 (5 by maintainers)
Top Results From Across the Web
mysql - Returning a value even if no result - Stack Overflow
MySQL has a function to return a value if the result is null. You can use it on a whole query: SELECT IFNULL(...
Read more >How to return Null or value when nothing is returned from Query
I have a simple query: SELECT name FROM atable WHERE a = 1. I want it to return Null if it finds nothing,...
Read more >How to SELECT Records With No NULL Values in MySQL
By far the simplest and most straightforward method for ensuring a particular column's result set doesn't contain NULL values is to use the...
Read more >Why does my SELECT query not return null values? [duplicate]
This happens because col IN (1,null) returns TRUE if col=1 , and NULL otherwise (i.e. it can never return FALSE ). Since NOT...
Read more >Resolve issues with Amazon Athena queries returning empty ...
However, when you query those tables in Athena, you get zero records. For example, your Athena query returns zero records if your table...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop 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
Top GitHub Comments
The code example is incorrect. You want to set
require: false
. The returned value should then benull
. If there is a still a problem please open a new issue as this relates to an older version of Bookshelf. If it does work please paste the revised code here for future reference. Thanks.Its a pretty common workflow to get and check if a model exists. This is a lot of code for such a common operation.
I think its best to provide another option to allow the success handler to return a null value.