How can I set firebase rule for switch button - javascript

in the realtime firebase
the child Button's value is 'true'
I want new value has to be only 'false' while it is 'true'
the opposite, it has to be true
how can I code up for firebase rule?
guessing to use data.exists() or 'true' or 'false' as string
but using data.exists() is further reaching to me.

Something like this should work:
".validate": "
(data.val() === 'true' && newData.val() === 'false') ||
(data.val() === 'false' && newData.val() === 'true')
"
Note that I use string values since you mentioned those in your question, but I usually would store boolean values in an actual boolean node.

Related

Using Ternary Operator to Handle Three Different Conditions

I'm using the ternary operator to handle importing data from SQL to Mongo for a variety of fields. For one particular field it's a little trickier than the others, because I want to handle three different conditions:
1 should port to true,
0 should port to false,
and null should port to null.
This is what I'm trying:
saved: data.saved && data.saved === 1 ? true : data.saved && data.saved === 0 ? false : null
Would this accomplish what I'm needing?
You could take a direct check for null and if not convert the numerical values to boolean.
value === null ? null : Boolean(value)
You can just coerce the value to boolean:
saved: (data === null) ? null : !!data

Typescript if and if not with boolean and string

I am editing an app using TypeScript React and Meteor. Currently I got the following state:
{user && ( ... )}
This shows some data only if the visitor is a logged in user. I although want to show the data only if the tournament is not cancelled. So I tried to add
{user && !tournament.state === 'cancelled' && ( ... )}
which does not work. I receive the following linter message:
Operator '===' cannot be applied to types 'boolean' and '"cancelled"'.
How can I solve this puzzle?
!tournament.state negates tournament.state, not the === that follows, resulting in true or false for that part, giving you true === 'cancelled' or false === 'cancelled'. Hence the issue with using===` on a boolean and a string.
Use !==:
{user && tournament.state !== 'cancelled' &&
Technically, you can use parentheses as well, but it wouldn't be idiomatic:
{user && !(tournament.state === 'cancelled') &&
TypeScript complains because you are trying to compare a boolean value: !tournament.state to a string: 'cancelled'.
The ! symbol is going to turn a truthy/falsy result into an boolean. This happens because assuming tournament.state is equal to 'cancelled':
!tournament.state = !'cancelled' = false
The triple equals operator then checks both value and type. Your code will not only be disliked by TypeScript, it is also incorrect, as every value of tournament.state is going to fail the check, because it will always end up as a boolean against a string. What you need is:
{user && tournament.state !== 'cancelled' && ( ... )}

How do I negate the parameter for AngularJS ng-if directive?

Quick Example:
There is a routed parameter (/Home/:isLoggedIn) that equates to true or false. (/Demo/#/Home/false) and a controller property
this.loggedIn = this.routeParams.loggedIn;
I have a view (Home.html) that has two elements, each with an ng-if attribute.
<div ng-if="home.loggedIn">
Logged In!
</div>
<div ng-if="!home.loggedIn">
Not Logged In...
</div>
If I navigate to /Demo/#/Home/true then the first element displays and the second does not.
If I navigate to /Demo/#/Home/false then the first element does not display NOR does the second one.
I would expect the !home.loggedIn parameter evaluate to true when the value of loggedIn is, in fact, false.
Any advice here?
It is quite obvious that he problem has its root to the fact that routeParams.loggedIn is a string.
So the solution is quite obvious:
// Change that:
this.loggedIn = this.routeParams.loggedIn;
// To this:
this.loggedIn = this.routeParams.loggedIn === 'true';
But why the weird behaviour ?
Why work not showing anything when loggedIn is 'false' ?
Well, here is why:
The ngIf directive uses the following toBoolean() function to convert its value to boolean:
function toBoolean(value) {
if (typeof value === 'function') {
value = true;
} else if (value && value.length !== 0) {
var v = lowercase("" + value);
value = !(v == 'f' || v == '0' || v == 'false' || v == 'no' || v == 'n' || v == '[]');
} else {
value = false;
}
return value;
}
If a string is passed to toBoolean() it converts it to lowercase and checks (among other things) if it equals 'false' (in which case it returns false). This is different than the default JavaScript implementation which interprets any non-empty string as true when casting to boolean.
So, let's examine the two cases for both ngIfs:
loggedIn === 'true'
ngIf1 evaluates home.loggedIn --> 'true' (string)
ngIf1 passes this value through toBoolean()
toBoolean('true') returns true (because it sees a string that can't match with any string considered falsy)
ngIf1 renders its content
ngIf2 evaluates !home.loggedIn <=> !'true' --> false (boolean)
(this happens because any non-empty string happens to evaluate to true)
ngIf2 passes this value through toBoolean()
toBoolean(false) returns false
ngIf2 does not render its content
loggedIn === 'false'
ngIf1 evaluates home.loggedIn --> 'false' (string)
ngIf1 passes this value through toBoolean()
toBoolean('false') returns false (because it sees a string that is considered falsy
ngIf1 does not render its content
ngIf2 evaluates !home.loggedIn <=> !'false' --> false (boolean)
(this happens because any non-empty string happens to evaluate to true)
ngIf2 passes this value through toBoolean()
toBoolean(false) returns false
ngIf2 does not render its content
So, this explains the "weird" behaviour (hopefully in an understandable way).
Problem is likely home.loggedIn is a string, when passed to ng-if it is probably evaluating and doing the conversion from string to bool to get the value "false" into false. In the expression evaluation before the value is passed through if you have !"false" that is actually false since any string is true, negating it becomes false.

if(xxx || xxx), what am I doing wrong?

Can someone explain to me what am I doing wrong?
function navMenuIntervalCheck() {
if (currentSiteUrl != "subsites/players.php" || currentSiteUrl != "subsites/itemshop.php") {
$.ajax({
url: currentSiteUrl,
cache: false,
success: function (dataForContainer) {
$('#container').html(dataForContainer);
}
});
}
screenControlCheck();
};
setInterval(function () {
navMenuIntervalCheck()
}, 5000);
When I run my website it refreshes even when currentSiteUrl==subsites/players.php
As x!='a' || x!='b' is always true, I guess you wanted && instead of ||.
Read || as OR and && as AND.
More details in the MDN on logical operators.
currentSiteUrl can only have one value, so it will always be that at least one of the values you're testing will not equal currentSiteUrl, making the if condition always true.
I think you meant to use && or you meant to do == with ||.
Your code says this :
Refresh when currentSiteUrl is different than subsites/players.php or different than subsites/itemshop.php.
So subsites/players.php is indeed different than subsites/itemshop.php
Use && instead of ||
Look at your if statement:
if (a != foo || a != bar)
Lets look at the possibilities:
a = foo. This will evaluate as true, because a != bar
a = bar. This will evaluate as true, because a != foo
a = anything else. This will evaluate as true, because a != foo
Your if statement always evaluates to true.
As others have already said, you want to replace your || with &&.
Let me throw a logical rule at you, called DeMorgan's Law. It's really useful when learning how to set up a good if statement.
!(a || b) === !a && !b
also
!(a && b) === !a || !b
What that says is: "Not (one event or another)" is the same thing as "Not one event and not another", and "Not (one event and another)" is the same thing as "Not one event or not the other".
I know this has been answered but thought it might help to add some additional information using your own code.
As said, switching logical operator from || to && will work:
if (currentSiteUrl != "subsites/players.php" && currentSiteUrl != "subsites/itemshop.php")
But why is that?
Using ||
The || logical operator returns true if either the first or second expression is true and only if both are false will it return false.
Hence:
if currentSiteUrl != "subsites/players.php" is true you end up in the if block
if currentSiteUrl != "subsites/players.php" is false and currentSiteUrl != "subsites/itemshop.php" is true you end up in the if block
There will never be another scenario as your variable currentSiteUrl can only hold a single value and as such one of the expressions will always be true causing you to always end up in the if block.
Using &&
Using the && logical operator on the other hand though returns false if either the first or second expression is false and only if both are true will it return true.
Hence:
if currentSiteUrl != "subsites/players.php" is true and currentSiteUrl != "subsites/itemshop.php" is true you end up in the if block
if currentSiteUrl != "subsites/players.php" is true and currentSiteUrl != "subsites/itemshop.php" is false you don't end up in the if block
if currentSiteUrl != "subsites/players.php" is false you don't end up in the if block
There will never be another scenario, because only if both expression are true will you end up in the if block and as soon as either expression is false will you not.
How are you getting the currentSiteUrl. A url is followed by protocol:://domain
Try using the follwoing property to get URL and then match it
window.location.href
This url will also include the http or https and domain name

Determining string length in Node JS when string may be null

I'm trying to learn Node and have the function:
this.logMeIn = function(username,stream) {
if (username === null || username.length() < 1) {
stream.write("Invalid username, please try again:\n\r");
return false;
} else {
....etc
and I'm passing it
if (!client.loggedIn) {
if (client.logMeIn(String(data.match(/\S+/)),stream)) {
I've tried both == and ===, but I'm still getting errors as the username is not detecting that it is null, and username.length() fails on:
if (username === null || username.length() < 1) {
^
TypeError: Property 'length' of object null is not a function
I'm sure that Node won't evaluate the second part of the || in the if statement when the first part is true - but I fail to understand why the first part of the if statement is evaluating to false when username is a null object. Can someone help me understand what I've done wrong?
length is an attribute, not a function. Try username.length
You're passing String(data.match(/\S+/)) as username argument, so when data.match(/\S+/) is null, you get "null" not null for username, as:
String(null) === "null"
So you need to change your condition:
if( username === null || username === "null" || username.length < 1 )
If you require a non-empty string, you can do a simple "truthy" check that will work for null, undefined, '', etc:
if (username) { ... }
With that approach, you don't even need the .length check. Also, length is a property, not a method.
Edit: You have some funkiness going on. I think you need to start with how you're passing in your username - I don't think that your String(data.match(/\S+/)) logic is behaving the way that you're expecting it to (credit to #Engineer for spotting this).
Your match expression is going to return one or two types of values: null or an Array. In the case that it's null, as #Engineer pointed out, you end up passing in "null" as a string, which should resultantly pass your username check later on. You should consider revising this to:
if (!client.loggedIn) {
var matches = data.match(/\S+/);
if (client.logMeIn(matches ? matches[0] : '',stream)) {
Regarding .length being equal to 1 in all cases - that doesn't honestly make a lot of sense. I would recommend adding a lot of console.log() statements to try and figure out what's going on.
Try
if( username === null || username.toString().length < 1 )
I used if( username === null || username.length < 1 ) and it failed at length check.

Categories

Resources