-1

I don't know what the problem is. It keeps returning an empty array. That is, movieIds is always empty.

function getMoviesInCinema(theCinema){
    var cinema = theCinema;
    var query = new Parse.Query("showing");
    var movieIds = [];

    query.equalTo("cinema", {
        __type: "Pointer",
        className: "Cinema",
        objectId: cinema
    });
    query.find().then(function(results) {
        if(results.length > 0){
            for (var i = 0; i < results.length; i++) {
                movieIds.push(results[i].get("movie"));
            }

        }
        else{
            console.log("Could be an error");
        }
    });
    return movieIds;

}
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
user3679294
  • 13
  • 1
  • 7

3 Answers3

2

That's because the query hasn't finished yet when you return from the function. You should make your function take a callback instead:

function getMoviesInCinema(theCinema, callback){
    var cinema = theCinema;
    var query = new Parse.Query("showing");

    query.equalTo("cinema", {
        __type: "Pointer",
        className: "Cinema",
        objectId: cinema
    });
    query.find().then(function(results) {
        if(results.length > 0){
            callback(results);
        }
        else{
            console.log("Could be an error");
        }
    });
}

Then call it as such:

getMoviesInCinema(theCinema, function(movieIds) {

    console.log(movieIds);

});
James M
  • 18,506
  • 3
  • 48
  • 56
  • I need the result of the query as a varable as such. var VariableName = resultofquery – user3679294 Aug 30 '14 at 09:21
  • @user3679294: You can't, at least not in a way that lets you usefully use the array immediately. Again, as James said, your function returns **before** the information is available. – T.J. Crowder Aug 30 '14 at 09:22
0

The issue here is that query.find() runs asynchronously. That means your getMoviesInCinema function returns before query.find calls your then callback.

Because query.find is asynchronous, your function cannot return the IDs directly. (It can return the array, but the array will be empty to start with.) Instead, it should also provide a Promise or allow for a callback.

Not sure what Promises lib you're using, so here's the option using a callback:

function getMoviesInCinema(theCinema, callback){
// Receive the callback --------------^
    var cinema = theCinema;
    var query = new Parse.Query("showing");
    var movieIds = [];

    query.equalTo("cinema", {
        __type: "Pointer",
        className: "Cinema",
        objectId: cinema
    });
    query.find().then(function(results) {
        if(results.length > 0){
            for (var i = 0; i < results.length; i++) {
                movieIds.push(results[i].get("movie"));
            }

        }
        else{
            console.log("Could be an error");
        }
        if (callback) {             // <=====
            callback(movieIds);     // <===== Call it
        }                           // <=====
    });
}

Usage:

getMoviesInCinema(42, function(movieIds) {
    // Use movieIds here
});
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
-3

Instead of using

movieIds.push(results[i].get("movie"));

Try using

movieIds.add(results[i].get("movie"));

It might solve your problem.

Prashant2329
  • 327
  • 3
  • 7
  • 21