How to access flow variables from javascript in power automate - javascript

I'm working on a power automate flow and I'm having some trouble. I copied some text to my clipboard in the flow and i asigned it to a variable in my flow. I'm trying to parse the variable using the javascript scripting feature but i'm getting a syntax error whenever I try to use the %variable% syntax in javascript. I used the variable button in the text editor they give you to add the variable to my script but even something simple as var res = %SimResults%; returns an error C:\Users\$me\AppData\Local\Temp\Robin\nkwrgcdoxrp.tmp(1, 11) Microsoft JScript compilation error: Syntax error
It seems that even though I Should be able to access my flow variables from Javascript it throws a syntax error when I try

You'll need to put quotes around it ...
var res = "%SimResults%"
... it treats the variables as more of a find and replace within your Javascript code, therefore, you need to do the work to ensure the correct encapsulation, etc. exists.
This may not solve your problem though depending on the complexity of what is contained within the variable.
If you have additional quotes, etc. you may need to escape them prior to putting them in the Javascript task IF they exist within the string ... just be mindful of that.

Related

Uncaught ReferenceError:some_string is not defined

In controller class, I have added
model.addObject("hostname", hostname);
and tried to catch it in my jsp page with
var hostname=<%=request.getAttribute("hostname")%> ;
Yet, this is throwing error
Uncaught ReferenceError:**some_string** is not defined
What can be done to avoid this?
Remember: You are not passing a variable from one program to another, you are programmatically generating JavaScript source code from JSP.
some_string is a variable name, but not one you've declared, so you get a ReferenceError.
You need to generate the JS source code which gives you the result you need.
For most cases, due to the compatibility between JS and JSON, you can use a JSON stringifier to generate the source code that creates your values (this is a good generic solution as it will do The Right Thing with quotes, new lines, arrays, etc).
Be careful as if the string contains </script> you need to escape the / to prevent it terminating the <script> element. Some JSON serializers will do this by default. I don't know if Java's will.

What's the best way to handle Eclipse errors on CouchDB Map/Reduce JavaScript functions?

As noted in Where to write and store mongoDB map/reduce functions in java project - Eclipse doesn't like a JavaScript (.js) file to only contain the following:
function(doc) {
if(doc.somekey) emit(doc._id, doc);
}
The keyword function is marked with an error:
Syntax error on token "function", Identifier expected after this token
This form is used in MapReduce, but perhaps it's not exactly valid JavaScript (I'm not a lawyer). Is there any way to allow this form?
edit it looks like it's a function expression instead of a function statement. ( MDN, ECMA-262 )
Non-solution: "Just add a function name" according to https://stackoverflow.com/a/11258388/185799 it seems that it's not important to economize on the size of these functions. However, adding a function name results in: {"error":"compilation_error","reason":"Compilation of the map function in the 'myView' view failed: Expression does not eval to a function."}
Possible solution? Add a function name or "module.exports = " at the top, and then remove it during a build phase, using grunt/gulp/yeoman or something related.
For now, what I am actually doing is using the literal form function anonymous(... and then doing a string replace just before calling synchronizeWithDb() to replace function anonymous( with function(. This doesn't answer my question, but works around it.
You have three bad options:
You can ignore the error since CouchDB can process this file (it will wrap it later to make it valid).
You can change the file extension but then you lose syntax highlight, code completion and error checks.
You can delete the error in the error view. It will stay deleted until you change the file the next time or you do a full build.
You may be able to configure Eclipse to stop validating the file.
There are two ways to implement #4:
You can ignore the resource. That makes this file invisible to Eclipse and all plugins. Which means you can't edit it anymore inside of Eclipse. Use Resource Filters for that.
You can check the per-project preferences for Validation rules for JavaScript. Maybe you can exclude the file.

Why does Eclipse complain about missing semicolon in my javascript?

I'm having an issue with Eclipse where it's complaining about missing semicolons in javascript code in a jsp file. Given the two lines below, Eclipse complains about the first line and indicates that there's a missing semicolon right before the closing curly brace. It has no complaints about the second line. I'd prefer to use the first way, but I'm annoyed with the warnings. Is there a different syntax that I should be using? I'm also using JQuery, so I don't know if this is contributing to the parsing error in Eclipse.
var isFoo = ${actionBean.isFoo}; // javascript type is boolean
var isFoo = '${actionBean.isFoo}'; // javascript type is string
To answer some of the questions people have posed in the comments...
This code is from a JSP file and actionBean refers to the Java action bean for this page.
"isFoo" is a member variable of the action bean. The syntax ${actionBean.isFoo} is JSP Expression Language (or EL for short) and it's used to evaluate a java variable in a JSP. In this case, my code takes the value from the java variable and assigns it to the javascript variable. The code works just fine, but Eclipse complains.
var isFoo = ${actionBean.isFoo};
This is not valid JavaScript, which is why you're getting a syntax error. I'm assuming the ${...} is supposed to do some interpolation in NetBeans, but Eclipse has no way of knowing that, and just tries to parse it as JavaScript. Unless there's an Eclipse plugin or setting for dealing with mixed code like this, you may just have to deal with seeing syntax errors.
Eclipse is complaining because it is not really Javascript, and it is trying to parse it like Javascript. If it is really annoying you could disable the warning in the preferences, under your own risk to not be notified in other circumstances:
var isFoo = ${actionBean.isFoo};
is not javascript, it is JSF

javascript: way to get the last value returned in the interpreter

I'm trying to find a way to programatically get the last value returned by the Javascript interpreter. Ruby's interpreter, to name an example, has the "_":
1 + 2 #=> 3
_ #=> 3
I would like to know if the same thing exists in Javascript.
EDIT:
Another way to maybe achieve this. Is there any syntax that supports the continuation of an expression in a newline? Something like this:
var a = \&
1 + 2;
a #=> 3
Some sort of combination of characters that tell the interpreter the expression continues in a newline (like the + for string concatenation).
PURPOSE:
Purpose of this research is to find if I can load a JSON data structure using a script tag and successfully assign it from outside of its scope, something like this:
<script> var json_struct = </script>
<script src="http://domain.com/myjsonfile.json" type='application/json' ></script>
which, by the way, doesn't work. Surprisingly :)
PURPOSE:
Purpose of this research is to find if I can load a JSON data structure
using a script tag and successfully assign it from outside of its scope
There is no construct in browser based javascript that can do this.
The reason is that browsers, since the earliest Netscape days, have always initiated the script compiler upon the closing of the script tag. Regardless if it's javascript, VBscript (IE only) or Tcl (with the appropriate plugin).
Which means that any statement that is incomplete will simply be treated as a syntax error. Each <script> tag is basically treated as a single file.
What you're trying to do is similar to this in Ruby:
a = require 'one_plus_two.rb'
which I don't think works in Ruby.
However, in non-browser environments that support modules like Node.js, the method that imports module does in fact return a value (usually an object). So you can do something like this in node.js:
var a = require('my_data_file.js');
Unfortunately, the require function only works on local files. But Node.js is open source so you can always fork it and modify require to be able to source from http:// like PHP.
Alas, if what you're trying to do is browser scripting then the above point is moot.

Run time error handling on lazy loaded javascript?

Does anyone have any ideas how to do error handling on lazy loaded javascript? I am using an approach in which an ajax request is called and the code is eval'd in global scope. When a runtime error is struck, it spits out the filename as my lazy loading script and the line number is the error line plus the line number of my eval in my loading script. This wouldn't be so bad except all the javascript files get combined into modules for sections of the site. A try catch around the javascript file itself wont catch runtime errors of the functions. Any ideas? Window.onerror doesn't provide the correct filename so it is out of the question. I need to catch it before it is hit.
I was thinking maybe I could programmatically include try catches around all the functions within the eval'd code (which is ugly), but since it is done at the window level I am not sure how to access the eval'd code specifically and dynamically. Sure if the javascript is an object named "Bob" I can access window.Bob but I need to do it dynamically.
I solved the issue, however it is not the most elegant solution. Essentially what I do is this:
1. After the site loads I look at all the objects that are in window and push them into an array. This basically says to my code, ignore these objects.
When I modularize my code I keep track of the length of the files and fileNames being place into a module.
The last line of the modulizer takes the fileLength array and lineLengths and calls a function in my error handling object;
The error handling code finds new objects in window. If they exist, set a property to match fileLengths and fileNames;
Recurse through the new objects and add decorate the functions to have try catches around them.
When one of those catches is hit, traverse upward and find the properties.
Calculate the file and line number based on the properties.
Output the new error based on the correct file and line number;
Yes ugly... but it works.

Categories

Resources