Skip to main content

Bug Tracker

Side navigation

#7577 closed bug (invalid)

Opened November 19, 2010 08:31PM UTC

Closed November 19, 2010 09:14PM UTC

Last modified September 24, 2012 07:09AM UTC

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

Reported by: barberboy@gmail.com Owned by:
Priority: undecided Milestone: 1.5
Component: unfiled Version: 1.4.4
Keywords: Cc:
Blocked by: Blocking:
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);

Attachments (0)
Change History (3)

Changed November 19, 2010 09:14PM UTC by jitter comment:1

_comment0: 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 [http://api.jquery.com/find/ find API doc] states \ > ... the selector expression is required in a call to .find()1290201634676866
resolution: → invalid
status: newclosed

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.

Changed November 23, 2010 09:19PM UTC by barberboy@gmail.com comment:2

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.

Changed September 24, 2012 07:09AM UTC by Akanksha comment:3

My code is giving error TypeError: $ is undefined at line in js file

$.each(feedData, function(entryIndex, entry){

Thanks

appointmentagent.com