Bug Tracker

Modify

Ticket #7577 (closed bug: invalid)

Opened 3 years ago

Last modified 19 months ago

TypeError: "query is undefined" is thrown for .find(undefined)

Reported by: barberboy@… Owned by:
Priority: undecided Milestone: 1.5
Component: unfiled Version: 1.4.4
Keywords: Cc:
Blocking: Blocked by:

Description

When the first argument to .find() is 'undefined', an error is thrown instead of returning a jQuery object with no elements. The error is:

TypeError: "query is undefined"

This regression is caused by a recent change to Sizzle to quote attribute selectors:  https://github.com/jeresig/sizzle/commit/aa6076c6a2b79acd2f8c67592f91206bbcb08f03

Example:

var a = {}; jQuery(document).find(a.foo);

Change History

comment:1 follow-up: ↓ 2 Changed 3 years ago by jitter

  • Status changed from new to closed
  • Resolution set to invalid

Thanks for taking the time to contribute to the jQuery project by writing a bug report!

After looking into this I determined that passing undefined to find() isn't a valid thing to do (besides not making much sense). The  find API doc states

... the selector expression is required in a call to .find()

If you have an application where you can prevent such things from happening (e.g. you don't control what gets passed to find()) you can use something like

.find(a.foo || "") to always have a valid selector.

Last edited 3 years ago by jitter (previous) (diff)

comment:2 in reply to: ↑ 1 Changed 3 years ago by barberboy@…

Hey jitter, thanks for looking into this issue!

I'm concerned about this issue because it is a regression that breaks backwards compatibility with 1.4.3 and is incorrect behavior according to the documentation on .find().

The API documentation for .find() says "The method accepts a selector expression of the same type that we can pass to the $() function." The documentation for the jQuery constructor says "calling the jQuery() method with no arguments returns an empty jQuery set."

With this regression, the behavior of .find() and $() are now inconsistent. Calling .find() should return an empty jQuery set instead of throwing an error.

While it's true that the behavior of .find() in this case was loosely documented, it's likely that there are plugins or other code depending on this behavior. Since this throws an error now it will very likely break other, seemingly unrelated elements on the page. This may prevent people from being able to upgrade.

comment:3 Changed 19 months ago by Akanksha

My code is giving error TypeError: $ is undefined at line in js file $.each(feedData, function(entryIndex, entry){

Thanks appointmentagent.com

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.