When you do console.log({ id : { $gte : 5 } });
You’re asking Node.js or Browser to show you something that needs to be converted into something visual. As the intended has a conversion factor each Javascript engine will treat it differently.
Node.js logs Strings, the browser allows pointers and therefore shows objects that can have new values since the console.log
ran.
Node.js does not place id
in quotes, but reacts to $
and puts this key inside quotes. If the object case was { id: { gte: 5 } }
I wouldn’t put it in quotes either.
These are variations between Javascript environments/engines.
How to prevent this?
Asks for the console.log
show you something that is already converted into String
. That is to say:
console.log(JSON.stringify({ id: { $gte: 5 } }));`
So you’ll have a homogeneous response, the same everywhere:
{"id":{"$gte":5}}
Good afternoon, but that’s just the visual, if you give a typeof on the object and on all Keys you see the types correctly. That will be in order
console.log(typeof obj) object
,console.log(typeof obj.id) object
andconsole.log(typeof obj.id.$gte) number
.– Chance
Well observed. I hadn’t thought so. The problem is that if I play the variable
obj
in a function, this function recognizes the property$gte
as string. But it must be for some mistake, not the one I asked the question. Thanks for the help.– Leandro Lima
If you can share your method, we can see what happens. You can use parseint or parseFloat to leave as type number.
– Chance
Keys are converted to strings, see examples here and the documentation here
– hkotsubo
$gte
being converted to string is kind of obvious, what’s weird there is the value5
have turned string. I just ran this code on Node V10.15.0 and it’s correct. O5
remains asNumber
.– fernandosavio