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.

fetch doesn't return null if query returns no results

See original GitHub issue

Returns 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:closed
  • Created 10 years ago
  • Reactions:1
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

7reactions
rhys-vdwcommented, Nov 9, 2015

The code example is incorrect. You want to set require: false. The returned value should then be null. 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.

1reaction
vjprcommented, Jun 18, 2013

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

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