Skip to content

scope method syntax loses parameters when used multiple times #7058

@doowb

Description

@doowb

What you are doing?

// simple model with scopes that take parameters
var Location = sequelize.define('Location', {
  state: types.STRING,
  city: types.STRING
}, {
  scopes: {
    state: function(state) {
      return {where: {state: state}};
    },
    city: function(city) {
      return {where: {city: city}};
    }
  }
});

// create a scopes array to be reused
var scopes = [
  {method: ['city', 'Springfield']},
  {method: ['state', 'OH']}
];

// do some queries
Location.scope(scopes).count().then(console.log);
Location.scope(scopes).findAll().then(console.log);

What do you expect to happen?

I expect the arguments in the scopes to be passed into the queries for both queries.

What is actually happening?

Only the first query is using the arguments because the internal method arrays on the scopes array are being modified here when using .splice.

Locally, I changed that to .slice so the array isn't mutated and it fixed my use case. I did a little digging in the issues, but not enough to determine if this has come up before. If this is a bug, I can try to submit a PR when I get a chance to make the changes and provide unit tests.

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueFor issues. An issue that is a good choice for first-time contributors.type: bugDEPRECATED: replace with the "bug" issue type

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions