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.

Model.fetchAll() is not firing fetching / fetched events

See original GitHub issue

I’ve come up with the use of event capture with model.on. Capturing events: creating / created, updating / updated, destroying / destroyed, but I can not fetch fetched / fetched from the model when executing fetchAll (). I’ve only proven that when I run fetch () from the model I catch the fetching / fetched events.

Into my model:

constructor: function() {
  bookshelf.Model.apply(this, arguments);

  // When trying to retrieve records (SELECT)
  this.on("fetching", function(model, attributes, options) {
    //options.query.where('type ', ' = ', ' user ');
    // Audit SELECT Queries in Model
    console.log('\nYou try to query user information ... \n');
  })

  // Successfully retrieve records (SELECT)
  this.on("fetched", function(model, attributes, options) {
    //options.query.where('type ', ' = ', ' user ');
    // Audit SELECT Queries in Model
    console.log('\nUser information is successfully consulted \n');
  })

Into my controller:

list: function list(req, res) {
  return userModel.fetchAll().then(function(records) {
    res.status(200).json(registers);
  }).catch(function(err) {
    res.status(400).json(err);
  })
},
find: function find(req, res) {
  return userModel
    .forge('id', req.params.id).fetch()
    .then(function(record) {
      res.status (200).json(record);
    }).catch(function(err) {
      res.status(400).json (err);
    })
}

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:4
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
ghostcommented, Mar 27, 2017

I’m also having the same issue, where I’m trying to run the following code in my model, in order to add another property to the response. The property relies on the fetched event which doesn’t fire when using the fetchAll method, but does run with fetch method.

  initialize() {
    this.on('fetched', (model) => {
      const isComplete = model.related('questionnaires').every(item => item.get('completed_at') !== null);
      model.set('is_complete', isComplete);
    });
  }
0reactions
priestoncommented, Feb 14, 2018

The reason is that fetching event refers to the models. Use “fetching:collection” instead for fetchAll.

Read more comments on GitHub >

github_iconTop Results From Across the Web

API Reference - Bookshelf.js
This method is similar to Model#fetchAll , but fetches a single page of results as specified by the limit (page size) and offset...
Read more >
Why is my fetch request not executing inside an event listener ...
When I call the fetch request function it logs to the console just fine, but as soon as I wrap it in an...
Read more >
Querying Data from a Database using fetchone() and fetchall()
The method only returns the first row from the defined table and If there are no tuples then it returns an empty list...
Read more >
fetch collection of records - Zoho
It is advisable to fetch all records only when absolutely needed. Fetching all records generates a load resulting in performance issues.
Read more >
A comprehensive guide to data fetching in React
We'll demonstrate how to fetch data in React, complete with code examples, ... triggered a GET API request since we invoked fetch with...
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