0

I have this Ionic code:

.factory('resultsFactory', function($http, $q, $rootScope, $firebaseArray, $timeout) { 
  var results = {};  

  function _all(){
    var d = $q.defer();
    var palabras =  $rootScope.textAreaOfrecer.toLowerCase().split(' ');
    var solicitudes = [];
    Promise.all(
              solicitudes = palabras.map(palabra => $firebaseArray(firebase.database().ref().child("solCompras").orderByChild("palabras/" 
                                               + palabra).equalTo(true) ) )
                     );

    var array = [].concat.apply([],solicitudes);

    d.resolve(array);

    return d.promise;       
  }

  results.all = _all;
  return results;
})

The problem is that the code: var array = [].concat.apply([],solicitudes); return an empty array.

json of solicitudes

Sanket
  • 19,295
  • 10
  • 71
  • 82
Ariel Brizi
  • 41
  • 1
  • 7

2 Answers2

0

Using [].concat.apply(solicitudes) instead. It may be that palabras is an empty array, then [].map(...) == [] can return Promise.all(solicitudes) directly.

function _all() {
        var palabras = $rootScope.textAreaOfrecer.toLowerCase().split(' ');
        return Promise.all(palabras.map((palabra) => {
            return $firebaseArray(firebase.database().ref().child("solCompras").orderByChild("palabras/" + palabra).equalTo(true));
        }));
    }
mbigras
  • 7,664
  • 11
  • 50
  • 111
holi-java
  • 29,655
  • 7
  • 72
  • 83
  • sorry i didnt understand – Ariel Brizi Feb 23 '17 at 01:58
  • check the collection `palabras` whether is an empty array? if true then palabras.map(...) always return [] – holi-java Feb 23 '17 at 02:01
  • debug map/concat function whether always return []? – holi-java Feb 23 '17 at 02:06
  • in fact this is not the problem. "array = [].concat.apply([],solicitudes);" array is what always keep in null. but "solicitudes" have elements. its an array of arrays that i want to unify – Ariel Brizi Feb 23 '17 at 02:15
  • the code "array = [].concat.apply([],solicitudes);" no problem.if `solicitudes` have elements & array is null then the `[].concat.apply([],solicitudes)` concat/apply function may be changed by someone.you can use `var strings=[].concat.apply([],['foo','bar']);console.log(strings);`before `array` assignment and see the result. – holi-java Feb 23 '17 at 02:25
  • In fact,you can use [].concat([]) directly because `palabras` is an array,using concat.apply is redundant. – holi-java Feb 23 '17 at 02:30
0
$scope.solicitudes = [{post: 'Loading..'}];


Promise.all(
      $scope.solicitudes = palabras.map(palabra => $firebaseArray(firebase.database().ref().child("solCompras").orderByChild("palabras/" + palabra
                                                   ).equalTo(true) ) )
             ).then( function (){
                               //http://www.jstips.co/en/flattening-multidimensional-arrays-in-javascript/
                               var myNewArray = $scope.solicitudes.reduce(function(prev, curr) {
                                 return prev.concat(curr);
                               });

                               $scope.solicitudes = myNewArray;
                         });
Ariel Brizi
  • 41
  • 1
  • 7