我在以下函数中查询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调用的URL时,我尝试通过复制并逐字将参数null粘贴到curl请求中,从而在终端中重现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'


它可以工作,但是当我对以前的URL进行快照时,我在null回调中得到了on('value')

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

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

最佳答案

您不能将带有查询参数的URL传递给Firebase构造函数。而是通过在位置引用上调用相应的方法来添加它们。例如。

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


我也强烈建议您迁移到“新” Firebase SDK,该文档记录在这里:https://firebase.google.com/docs/database/web/start。使用较新的SDK(您使用的SDK至少已经使用了一年)将确保您找到更多有用的文档,并且有大量的开发人员可以提供帮助。

09-26 20:15
查看更多