Karma
Karma

Reputation: 2226

Boolean query parameter being treated as string by Sails.js

My 'user' model (table) has an attribute (column) named 'active' of type: 'boolean'. While querying the Blueprint API:

CASE 1: http://localhost:1337/user?active=true

returns no data. However,

CASE 2: http://localhost:1337/user?active=1

behaves the desired way.

I tried tracing the request, but couldn't go beyond actionUtil.js. The where returns {active: 'true'} in case 1 and {active: '1'} in case 2. It's strange that the number as a string gets converted to number (1) but boolean as a string does not. Since the ORM has generated this column with type tinyint(1), I am sure there is '1' to 1 conversion happening somewhere. I just need to add another conversion for boolean to 0/1. Any help?

If anyone could explain the request trace, that'd be really great! I don't know where the exec() function in find.js is defined.

Upvotes: 3

Views: 1568

Answers (1)

sgress454
sgress454

Reputation: 24948

I am sure there is '1' to 1 conversion happening somewhere.

Yup--in Javascript. Fire up your Javascript console and try:

"1" == true
"0" == false 
"true" == true
"false" == false

The first two will return true. The second two will return false.

If you're going to use blueprints with querystrings, you're going to have to deal with the fact that everything in the querystring is a string. That means using 1 and 0 instead of true and false, among other things (like being careful with ===).

Upvotes: 3

Related Questions