Kllicks
Kllicks

Reputation: 101

Node.JS and PostgresSQL query issue with pattern matching

I would like to write a query that selects a list of companies based on type and location. The type will be a dropdown. So it wouldn't be case sensitive or anything. However, for company location I'd like to be able to find any location containing the string put in the form field. Here's what I have:

static getCompanyByLocation(company_type, company_location) {
        return db.any(
            `select * from companies where company_type=$1 and company_location ilike $2`, [company_type, company_location]
        )
}

If I search for %$2%, which I'd hoped it would search for anything containing the string that replaces $2. Instead I get an error. Not sure of the syntax here and would appreciate any help.

Upvotes: 1

Views: 127

Answers (1)

Kllicks
Kllicks

Reputation: 101

Nevermind I found a way around my problem. On the JavaScript side I just added %% to the variable when I defined it and then I passed that value into the query for location.

Upvotes: 1

Related Questions