Underscore _.where function return only one item
LionelGuerin opened this issue · comments
Hi,
I'm using and reproducing this with meteor on Mac (version 14.2.1) and ubuntu (version 20.04.6)
Since Meteor 2.15, with the upgrade of underscore from 1.0.13 to 1.6.1 .
The return of the function _.where(array, {criteriaToFind}) seems to always be the first element to correspond to the search.
In previous meteor versions, the function _.where used to return an array of every items find.
I made a repository to reproduce the problem.
https://github.com/LionelGuerin/meteor_underscore_where
It's composed of three folders to tests with meteor 2.15 , meteor 2.14 and with vanilla underscore NPM.
For meteor 2.15
git clone git@github.com:LionelGuerin/meteor_underscore_where.git
cd underscore_where/meteor_underscore
npm i
meteor
Go to your browser then click on "Click Me"
You should see the following server log
underscoreTest { randomid: '0DVVuahGdr', tofind: true }
For meteor 2.14
git clone git@github.com:LionelGuerin/meteor_underscore_where.git
cd underscore_where/2-14_meteor_underscore
npm i
meteor
Go to your browser then click on "Click Me"
You should see the following server log
underscoreTest [
{ randomid: '0DVVuahGdr', tofind: true },
{ randomid: '6DAPnPUk7k', tofind: true }
]
For vanilla underscore NPM
git clone git@github.com:LionelGuerin/meteor_underscore_where.git
cd underscore_where/2-14_meteor_underscore
npm i
node underscore_vanilla.js
You should see the following log
underscoreTest [
{ randomid: '0DVVuahGdr', tofind: true },
{ randomid: '6DAPnPUk7k', tofind: true }
]
I have seen another underscore issue with _.intersection, but it seems to be a different problem.
If you need any more information, i will be happy to help ;)
This hit me too -- lots of places where I was using _.where
to filter an array, now they all return an object. Happy to help with a bug fix.
In this PR, things changed.
Before, _.where
was implemented using either _.filter
(returning every item) or with _.find
(returning only one, the first that matches)
What you can do is override it like this:
_.where = function(obj, attrs) {
return _.filter(obj, _.matches(attrs));
};
I'll test it here in your repo and leave an edit.
edit:
it works
Yep, we're seeing this bug pop up in dependants of meteor/underscore
. One example is aldeed/autoform
v6.3.0
.
https://github.com/Meteor-Community-Packages/meteor-autoform/blob/d8f8b6a6e36079c054976ca8bda7bec97f548b4f/autoform-api.js#L504-L505
An array is expected here, but an object is returned from _.where()
, so we're left with
_.where(/* ... */)[0]
{ /* ... * /}[0] // <- undefined
Yes, I know aldeed/autoform@6.3.0
is outdated 😅 and we're working on updating it now, but it sounds like there are other affected dependants :).
Would it be possible to revert the _.where
change? We could help with a PR if you like.
Here's the where
changeset:
47949d9#diff-1cb90728ba18568f3c72e6685a2efa7a5926eb93b30991df6a3df86d2ed5da0cL266-L273
Thanks for all the hard work!
Just in case anyone needs a short-term workaround - Meteor allows for override packages, even core, by placing the package source code in the packages
folder in your application's root.
[your_application_root]/packages/[package-name]
ex:
[your_application_root]/packages/underscore/
https://guide.meteor.com/writing-atmosphere-packages#local-packages