Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
firebase.on值在不存在数据时不返回null_Firebase_Angular_Ionic2 - Fatal编程技术网

firebase.on值在不存在数据时不返回null

firebase.on值在不存在数据时不返回null,firebase,angular,ionic2,Firebase,Angular,Ionic2,我在angular/ionic 2中使用firebase,我需要检查一个值是否存在,如果不存在,我需要创建它。但是firebase没有像在中所述的那样返回null,并且我的检查因此没有运行 subscribeToOffer(uid, offerID) { var path = 'users/' + uid + '/offers/' + offerID; this.rootRef.child(path).on('value', subscribed => {

我在angular/ionic 2中使用firebase,我需要检查一个值是否存在,如果不存在,我需要创建它。但是firebase没有像在中所述的那样返回null,并且我的检查因此没有运行

subscribeToOffer(uid, offerID) {
    var path = 'users/' + uid + '/offers/' + offerID;

    this.rootRef.child(path).on('value', subscribed => {
        if (subscribed.val() !== null) {
            console.log('subscribed');
        } else {
            console.log('not subscribed');
        }
    });
}

这里我为您编写了一个简单的函数,如果offer id存在,它将返回true,在其他情况下返回false

subscribeToOffer(userId,offerId){
        var userRef = new Firebase(FBURL+'users');
        var userOfferRef = userRef.child(userId).child("offers");
        return userOfferRef.on("value", function(snap) {
          var offerIds = snap.val();
          return !!offerIds.hasOwnProperty(offerId);
        });
  };

如果它不返回null,那么它返回什么?你确定路径是正确的吗?也许可以添加一个你在问题中使用的数据样本(作为文本,而不是图像),它根本不会返回,什么也不会发生。路径是正确的,因为当提供存在时,它会运行,但当没有提供时,它不会执行任何操作,我需要返回一些内容来执行我的else语句。您是否尝试过:或查看过:嗨,是的,尝试过,hasChild()并在没有运气的情况下修改了路径也尝试了exists()。同样的情况也会发生。我有一个console.log(subscribed.val());当没有子对象时,它不会运行,它应该返回null。firebase版本241