Samantha J T Star
Samantha J T Star

Reputation: 32828

Getting an error "has no method 'contains'"

I am trying to use the following code snippet:

console.log(config.url);
if (config.method === 'GET' && config.url.contains("/Retrieve")) {

It outputs:

/Content/app/home/partials/menu.html app.js:255
TypeError: Object /Content/app/home/partials/menu.html has no method 'contains'
    at request (http://127.0.0.1:81/Content/app/app.js:256:59)

However this is giving me and error with the word ".contains". Anyone have any idea why I am getting this message?

has no method 'contains'

Does anyone have any idea why I might be getting this message?

Upvotes: 0

Views: 1932

Answers (3)

James Thomas
James Thomas

Reputation: 178

contains() is not a native JavaScript method; it's used by JS libraries such as jQuery though.

If you're just using pure JavaScript then you could use a method such as indexOf().

Upvotes: 1

ssimeonov
ssimeonov

Reputation: 1426

The problem is that contains is a jQuery function so you can apply it only on jQuery objects.

If you want to use pure javascript you have to use indexOf function

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1075029

Because in JavaScript, strings don't have a contains method. You can use indexOf:

if (config.method === 'GET' && config.url.indexOf("/Retrieve") !== -1) {

Note that that's case-sensitive. To do it without worrying about capitalization, you can either use toLowerCase():

if (config.method === 'GET' && config.url.toLowerCase().indexOf("/retrieve") !== -1) {

...or a regular expression with the i flag (case-Insensitive):

if (config.method === 'GET' && config.url.match(/\/Retrieve/i)) {

Upvotes: 8

Related Questions