1196
Views
5
Comments
Solved
Async JavaScript: Uncaught ReferenceError: $resolve is not defined
Question

I need help, please...

I'm working on mobile client-side AddImageWatermark function for my Image Utils Mobile component.

All other of my async JS function works with $resolve();

Only this function throw error: $resolve is not defined.

OML is attached.


Platform Server: P10 Version 10.0.302.0 (Java stack)

Service Studio: 10.0.407.0



Code Snippet

var imgW = document.createElement("img");

imgW.onload = function () {
  var watermark = document.createElement("img");
  
  watermark.onload = function () {
    var canvas = document.createElement('canvas');
    var ctx = canvas.getContext("2d");
    var opacity = (255/(100/$parameters.opacity));
  
    // Apply Transparency
    if ($parameters.opacity != 100) {
      canvas.width = watermark.width || watermark.offsetWidth;
      canvas.height = watermark.height || watermark.offsetHeight;

      ctx.drawImage(watermark, 0, 0);
          
      var image = ctx.getImageData(0, 0, canvas.width, canvas.height);
      var imageData = image.data,
          length = imageData.length;
      
      for(var i=3; i < length; i+=4){  
       imageData[i] = (imageData[i]<opacity)?imageData[i]:opacity;
      }
      image.data = imageData;
      ctx.putImageData(image, 0, 0);
      watermark.onload = null;
      watermark.src = "";
      watermark.src = canvas.toDataURL();
      // assign img attributes to the transparent watermark
      // because browsers recalculation doesn't work as fast as needed
      watermark.width = canvas.width;
      watermark.height = canvas.height;          
    }
    
    canvas.width = imgW.width || imgW.offsetWidth;
    canvas.height = imgW.height || imgW.offsetHeight;
    ctx.drawImage(imgW, 0, 0);
  
    var position = $parameters.position,
    x = 0,
    y = 0;
  
    if(position.indexOf("top")!=-1)
      y = 10;
    else
      y = canvas.height-watermark.height-10;
  
    if(position.indexOf("left")!=-1)
      x = 10;
    else
        x = canvas.width-watermark.width-10;

    ctx.drawImage(watermark, x, y);
  
    var imageWatermarked = canvas.toDataURL();    
    imageWatermarked = imageWatermarked.substring(imageWatermarked.indexOf(',') + 1);
    
    $parameters.watermarkedImage = imageWatermarked;       
    $resolve();
  };
  
  watermark.setAttribute('crossOrigin', 'anonymous');
  watermark.src=$parameters.watermarkPath;
};

imgW.setAttribute('crossOrigin', 'anonymous');
imgW.src="data:image/png;base64,"+$parameters.image;
TestResolve.oml
2014-09-02 18-03-17
Sérgio Silva
Staff
Solution

Hi Harlin,

Thanks for reporting this issue! You've uncovered a bug related to how we infer a couple of things from the JS code you write in the node, e.g. whether $resolve is being used or not. We'll work on fixing this!

For now, and as a workaround, please remove all comments from the code you wrote and you should be alright. 

Let me know if that works for you.

Cheers,

Sérgio

2024-03-25 06-19-08
Harlin Setiadarma
2014-09-02 18-03-17
Sérgio Silva
Staff
Solution

Hi Harlin,

Thanks for reporting this issue! You've uncovered a bug related to how we infer a couple of things from the JS code you write in the node, e.g. whether $resolve is being used or not. We'll work on fixing this!

For now, and as a workaround, please remove all comments from the code you wrote and you should be alright. 

Let me know if that works for you.

Cheers,

Sérgio

2024-03-25 06-19-08
Harlin Setiadarma

Hi Sérgio,

Yes removing all comments allow $resolve() to work..

Thanks.

UserImage.jpg
Stas Pl

Why this bug still exists???????? 

2024-10-15 10-08-31
Tiago Ribeiro
Champion

Just lost a couple of hours in a similar issue. In my case it was due to a missing ' inside a string.

This code does not work:

console.log(`Random comment '${var}`);

$resolve();

While this does (notice the ' after {var});

console.log(`Random comment '${var}'`);

$resolve();


Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.