I've written these scripts to run a function conditioned on the values of an HTTP
GET
request. The history
var is defined correctly when I run two tests on the page:
history == 0
history == 1
The problem is allMessage function will always run, whether the value is 0
or 1
.
If I try to reverse the function ( limitMessage()
on the if
branch, allMessage()
on the else
branch) limitMessage()
is the one that runs. I have no idea why the if else
condition doesn't work as expected.
//*if else condition*
var history = "<? php echo isset($_GET['history']) ? $_GET['history'] : 0; ?>";
if (history != 1) {
allMessage();
} else {
limitMessage();
}
//*limitMessage*
function limitMessage() {
firebase
.firestore()
.collection(collection)
.doc(doc)
.collection(collection)
.orderBy("time", "desc")
.limit(10)
.onSnapshot(function (querySnapshot) {
querySnapshot
.docChanges()
.reverse()
.forEach(function (change) {
var data = {
id: change.doc.id,
a: change.doc.data().a,
b: change.doc.data().b,
c: change.doc.data().c,
d: change.doc.data().d,
e: change.doc.data().e,
f: change.doc.data().f,
};
if (change.type === "added") {
msgData(data);
}
if (change.type === "modified") {
msgData(data);
}
});
});
}
//*allMessage*
function allMessage() {
firebase
.firestore()
.collection(collection)
.doc(doc)
.collection(collection)
.orderBy("time", "asc")
.onSnapshot(function (querySnapshot) {
querySnapshot.docChanges().forEach(function (change) {
var data = {
id: change.doc.id,
a: change.doc.data().a,
b: change.doc.data().b,
c: change.doc.data().c,
d: change.doc.data().d,
e: change.doc.data().e,
f: change.doc.data().f,
};
if (change.type === "added") {
msgData(data);
}
if (change.type === "modified") {
msgData(data);
}
});
});
}
It is good practice to check the output of var
by using typeof
.
You can then decide how to manage the if/else statement.
You might not be handling the output correctly and why it is always running, whether the value is 0 or 1.
console.log(typeof 'checkme');
// expected output: "string"
console.log(typeof 12);
// expected output: "number"
console.log(typeof undeclaredVariable);
// expected output: "undefined"
console.log(typeof true);
// expected output: "boolean"
It can be confusing to write good if/else statements. To make it easier, I use if
as equal to
rather than not equal
as I find it more intuitive when reading code and spotting issues.
var history=1;
if (history == 1) {
console.log("yes");
} else {
console.log("no");
}
Here are some of the comparison operators to try:
+-----------+-----------------------------------+-----------+---------+
| == | equal to | 5 == 8 | false |
| | | 5 == 5 | true |
| | | 5 == "5" | true |
| === | equal value and equal type | 5 === 5 | true |
| | | 5 === "5" | false |
| != | not equal | 5 != 8 | true |
| !== | not equal value or not equal type | 5 !== 5 | false |
| | | 5 !== "5" | true |
| | | 5 !== 8 | true |
+-----------+-----------------------------------+-----------+---------+
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.