Firebase orderBy查询返回null

时间:2017-06-01 06:53:11

标签: javascript firebase firebase-realtime-database

我在以下函数中查询FB:

  getLimit: function(artistId, limit) {
    console.log(FBURL + 
                        'songs?orderBy=\"artist_timestamp\"&startAt=\"' + 
                        artistId + 
                        '_\"&endAt=\"' + 
                        artistId +
                        '_9999\"&limitToLast=' + 
                        limit + 
                        '');

     ref= new Firebase(FBURL + 
                        'songs?orderBy=\"artist_timestamp\"&startAt=\"' + 
                        artistId + 
                        '_\"&endAt=\"' + 
                        artistId +
                        '_9999\"&limitToLast=' + 
                        limit + 
                        '');

     console.log(ref);

      ref.on('value', (snapshot) => {
        console.log("hello");
        var val = snapshot.val();
        console.log(val);
     });
     results = $firebaseArray(ref);
        results.$loaded().then(function() {
    console.log("loaded record:", results.$id);

   // To iterate the key/value pairs of the object, use angular.forEach()
   angular.forEach(results, function(value, key) {
      console.log(key, value);
   });
 });
   return results;
  }

当我看到我使用第一个console.log呼叫的网址时,我尝试通过复制重新生成null结果,并将参数逐字地粘贴到new Firebase()作为卷曲请求如下:

curl "the-exact-url-except-add-dot-json"

这是我传递给new Firebase()的基本格式:

https://my-proj.firebaseio.com/songs?orderBy="artist_timestamp"&startAt="foo_"&endAt="foo_9999"&limitToLast=7

所以当我卷曲

curl 'https://my-proj.firebaseio.com/songs.json?orderBy="artist_timestamp"&startAt="foo_"&endAt="foo_9999"&limitToLast=7'

它有效,但当我拍摄以前网址的快照时,我会在null回调中获得on('value')

我得到了我期望的结果,它是song键和对象的对象。

但是,当我同时获得ref的快照以及转换为firebaseArray并调用回调$loaded后,我在两种情况下都获得了null。我不确定是什么使结果出现在终端但不在代码中。

1 个答案:

答案 0 :(得分:1)

您无法将包含查询参数的网址传递到Firebase构造函数中。而是通过调用位置引用上的相应方法来添加它们。 E.g。

ref = new Firebase(FBURL + 'songs');
var query = ref.orderByChild("artist_timestamp")
               .startAt(artistId)
               .endAt(artistId+'_9999')
               .limitToLast(limit);

高度建议您迁移到" new" Firebase SDK,在此处记录:https://firebase.google.com/docs/database/web/start。使用更新的SDK(您正在使用的SDK至少已有一年)将确保您找到更多有用的文档,并且有更多的开发人员可以提供帮助。