WP-API / node-wpapi

An isomorphic JavaScript client for the WordPress REST API
http://wp-api.org/node-wpapi/
MIT License
1.68k stars 190 forks source link

"_.pluck is not a function" error #193

Closed joneslloyd closed 8 years ago

joneslloyd commented 8 years ago

Great JS client, and works really well.

One issue I've found is that when using it in the WP Editor area (as part of a JS-fuelled WordPress plugin using React), I get the following console errors:

load-scripts.php?c=0&load[]=hoverIntent,common,admin-bar,heartbeat,autosave,wp-ajax-response,jquery…:380 Uncaught TypeError: _.pluck is not a functiondetach @ load-scripts.php?c=0&load[]=hoverIntent,common,admin-bar,heartbeat,autosave,wp-ajax-response,jquery…:380render @ load-scripts.php?c=0&load[]=hoverIntent,common,admin-bar,heartbeat,autosave,wp-ajax-response,jquery…:380render @ load-scripts.php?c=0&load[]=hoverIntent,common,admin-bar,heartbeat,autosave,wp-ajax-response,jquery…:416init @ load-scripts.php?c=0&load[]=hoverIntent,common,admin-bar,heartbeat,autosave,wp-ajax-response,jquery…:417i @ jquery.js:1fireWith @ jquery.js:1ready @ jquery.js:1K @ jquery.js:1
load-scripts.php?c=0&load[]=hoverIntent,common,admin-bar,heartbeat,autosave,wp-ajax-response,jquery…:419 Uncaught TypeError: _.pluck is not a function

and

load-scripts.php?c=0&load[]=hoverIntent,common,admin-bar,heartbeat,autosave,wp-ajax-response,jquery…:663 Uncaught TypeError: Cannot read property 'hasClass' of undefined

I've extensively tested and turned everything else off/on, and only when I remove the node-wpapi library from my webpack file (therefore not including it in my project) do the above errors stop.

kadamwhite commented 8 years ago

Interesting indeed! You're enqueuing a single, webpack'd file, then, I take it? I'm perplexed as to why the lodash reference would be conflicting with anything. Are you defining aliases for _ anywhere in your webpack config, by any chance?

joneslloyd commented 8 years ago

Yes! Re: The single webpack'd file. And only jQuery! Pretty odd...

On 26 Jul 2016 2:21 am, "K.Adam White" notifications@github.com wrote:

Interesting indeed! You're enqueuing a single, webpack'd file, then, I take it? I'm perplexed as to why the lodash reference would be conflicting with anything. Are you defining aliases for _ anywhere in your webpack config, by any chance?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/WP-API/node-wpapi/issues/193#issuecomment-235135439, or mute the thread https://github.com/notifications/unsubscribe-auth/AA84M-zRqBAnddK34RaS-5vWt8nM9gBJks5qZWEPgaJpZM4JUnS_ .

joneslloyd commented 8 years ago

Ah, I wonder if it's related to this: https://core.trac.wordpress.org/ticket/35181 (Lodash needing to be put into noConflict mode due to Underscores conflicts) ?

joneslloyd commented 8 years ago

Ah - solved it (for me) !

In: /wpapi/lib/mixins/filters.js /wpapi/lib/mixins/parameters.js /wpapi/lib/constructors/wp-request.js /wpapi/tests/integration/categories.js

I changed, in each instance, var _ = require( 'lodash' ); to:

 var ld = require( 'lodash' );
 var _ = ld.noConflict();

Now everything works perfectly :)

kadamwhite commented 8 years ago

Awesome, thanks for the update. Lodash is going away in favor of individual Lodash sub modules in 0.9, so this ought not be a problem again in the future; but good to know what's the reason was!

jcicero518 commented 6 years ago

Thanks for this guys. I ran into the same issue ~2 years later making use of lodash's pluck method in a React / Redux driven WP plugin. I was importing it -

import __ from "lodash";

I've been habitually setting "this" to _ in methods, probably from my Angular days, I thought this was the conflict initially. This fixed it:

import ld from "lodash"; let __ = ld.noConflict();

ditero commented 5 years ago

Huu! what a relieve thanks guys I've been fiddling with this error: "_.pluck is not a function" error" the whole day.

kadamwhite commented 5 years ago

:grin: Glad this issue lives on and helps people!