So I have a "schema" that looks like this:
- orders
- <$uid>
- orderid
- orderid
- transactions
- <$uid>
- transactionid
I've tried a few ways to access, say, orders/$uid/orderid
but it looks like if the key itself is null, the result of .once
or .on
is never created, even initially.
This is problematic because when adding something to cart, I need to see if user already has a cart / I need to check if it's empty.
I've added a falsy value (0
, specifically) as a default and now accessing ref.child('cart/' + user.uid).once()
does in fact return null
.
The trade-off seems to be that I need to "seed" a new DB with default values or else no events are fired.
Am I missing something or is this a common pattern? If not, what is a better way to deal with null keys essentially being non-existent and not triggering events?
You need to do the following:
ref.child("orders").orderByChild("orderid").equalTo(orderid).once("value",snapshot => {
var datas=snapshot.val();
if(datas){
console.log("it exists");
}
else{
console.log("doesn't exist");
}
});
This is the only way, the above will check if the snapshot that is retrieved by once()
or on()
exists.