Question

in the following .then structure, I am having problems returning the image object to the next .then.

At the point where I return image, it is no longer available because it is out of the initial scope.

Is there a correct way to make it available there, or am I off with my coding here?

  .then(function(image) {        


    image.data().then(function(result) {
        var base64 = result.toString("base64");
        var largeImage = new Parse.File("l.jpg", { base64: base64 });
        largeImage.save().then(function(result,image) {
            post.set('largeImageUrl',result.url());
            return image;    /// <-- undefined. How can access the image object here?
        });
    });



  }).then(function(image) {
Was it helpful?

Solution

Your original image variable is not out of scope; variables are visible to all functions defined within the same block, even if the functions are defined within other functions. That is, a variable's scope is the function it is defined in. If there are variables of the same name across various functions, the inner-scoped function will have a new reference for that scope and no longer reference the outer function's value.

However, the original image variable is shadowed by another variable also called image in the innermost scope, the function(result,image) you pass to largeImage.save().then(). If you still need a reference to the outer image variable, you should rename your inner image variable to something else.

Since that innermost image variable is undefined, it looks to be a leftover from a copy+paste, and might not even belong on the parameter list of function(result,image) in the first place.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top