Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Using hooks with arrays causes after hooks to run x number of times. #218

Copy link
Copy link
@marshallswain

Description

@marshallswain
Issue body actions

I discovered this odd behavior by using an afterHook on create with a beforeHook on another service's create. I'm not sure where this is happening, yet. Update: it's due to how some of the adapters handle arrays.

Below is an example to duplicate the issue. Switch between the create statements and you'll see the difference.

var feathers = require('feathers');
var feathersHooks = require('feathers-hooks');
var bodyParser = require('body-parser');
var rest = require('feathers-rest');
var memory = require('feathers-memory');

// Create a feathers instance.
const app = feathers()
  .configure(feathersHooks())
  .configure(rest())
  // Turn on JSON parser for REST services
  .use(bodyParser.json())
  // Turn on URL-encoded parser for REST services
  .use(bodyParser.urlencoded({ extended: true }));

// Create an in-memory Feathers service with a default page size of 2 items
// and a maximum size of 4
app.use('/todos', memory());
app.use('/tests', memory());

app.service('/todos').after({
  create: [
    function(hook){
      console.log('todos:after');
      return new Promise(function(resolve){
        console.log('hi');
        app.service('/tests').create({
          text: 'Server test'
        }).then(function(test){
          resolve(hook);
          console.log(test);
        });
      });
    }
  ]
});

app.service('/tests').before({
  create: [
    function(hook){
      console.log('tests:before');
      return new Promise(function(resolve){
        console.log('hi');
        resolve(hook);
      });
    }
  ]
});

// Create Todos
app.service('todos').create([
  {
    text: 'Server todo',
    complete: false
  },
  {
    text: 'Client todo',
    complete: false
  }
]);

// Create a dummy Todo
// app.service('todos').create({
//   text: 'Server todo',
//   complete: false
// });

app.service('todos').find().then(function(todos) {
  console.log('Found todos', todos);
});

// Start the server.
var port = 3030;

app.listen(port, function() {
  console.log(`Feathers server listening on port ${port}`);
});

Here's the output when creating a single object:

Bitovis-MacBook-Pro:mma-api Bitovi$ node test
todos:after
hi
tests:before
hi
Found todos [ { text: 'Server todo', complete: false, id: 0 } ]
{ text: 'Server test', id: 0 }

Here's the output when using create with the array:

todos:after
hi
todos:after
hi
tests:before
hi
tests:before
hi
Found todos []
{ text: 'Server test', id: 0 }
{ text: 'Server test', id: 1 }
todos:after
hi
tests:before
hi
{ text: 'Server test', id: 2 }
Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.