I'm by no means what I would call a "developer" but dabble quite a bit. I'm working on some Apps Script code to query an API and push the results into SQL. I have most of the bits working but I've noticed that while I'm debugging in the Apps Script editor, when I step into the following line of code, the editor throws the "could not connect to server message at the top.
var response = UrlFetchApp.fetch(clientApiURL,options);
var resultSet = JSON.parse(response.getContentText()); <-- this is the line that is crashing the IDE
Anyone know how to better debug this? When I'm not debugging it, the code seems to behave and function properly. But with this API, not all objects are formatted the same way, so I like to use the debugger to inspect them. I can do that when the editor crashes.
Any help/insight would be super appreciated. I've also pasted below the value of response.getContentText()
{"result":{"lead":[{"id":"332","accountID":null,"ownerID":null,"companyName":"","title":null,"firstName":"RYAN","lastName":"CAVANAUGH","street":null,"city":null,"country":null,"state":null,"zipcode":null,"emailAddress":"email#here.com","website":null,"phoneNumber":null,"officePhoneNumber":null,"phoneNumberExtension":null,"mobilePhoneNumber":null,"faxNumber":null,"description":null,"campaignID":"789934082","trackingID":"202003_5e6fa18a87853a69eb306910","industry":null,"active":"1","isQualified":"1","isContact":"1","isCustomer":"1","status":"4","updateTimestamp":"2020-05-08 20:24:48","createTimestamp":"2020-05-03 20:23:29","leadScoreWeighted":"23","leadScore":"26","isUnsubscribed":"0","leadStatus":"customer","persona":"","product_5e554b933fb5b":""}]},"error":null,"id":"5222020","callCount":"215","queryLimit":"50000"}
This will reproduce the error:
function test(){
var obj={"result":{"lead":[{"id":"332","accountID":null,"ownerID":null,"companyName":"","title":null,"firstName":"RYAN","lastName":"CAVANAUGH","street":null,"city":null,"country":null,"state":null,"zipcode":null,"emailAddress":"email#here.com","website":null,"phoneNumber":null,"officePhoneNumber":null,"phoneNumberExtension":null,"mobilePhoneNumber":null,"faxNumber":null,"description":null,"campaignID":"789934082","trackingID":"202003_5e6fa18a87853a69eb306910","industry":null,"active":"1","isQualified":"1","isContact":"1","isCustomer":"1","status":"4","updateTimestamp":"2020-05-08 20:24:48","createTimestamp":"2020-05-03 20:23:29","leadScoreWeighted":"23","leadScore":"26","isUnsubscribed":"0","leadStatus":"customer","persona":"","product_5e554b933fb5b":""}]},"error":null,"id":"5222020","callCount":"215","queryLimit":"50000"}
var resultSet = JSON.parse(obj);
}
Taking a look at the problem:
function test(){
var obj={"result":{"lead":[{"id":"332","accountID":null,"ownerID":null,"companyName":"","title":null,"firstName":"RYAN","lastName":"CAVANAUGH","street":null,"city":null,"country":null,"state":null,"zipcode":null,"emailAddress":"email#here.com","website":null,"phoneNumber":null,"officePhoneNumber":null,"phoneNumberExtension":null,"mobilePhoneNumber":null,"faxNumber":null,"description":null,"campaignID":"789934082","trackingID":"202003_5e6fa18a87853a69eb306910","industry":null,"active":"1","isQualified":"1","isContact":"1","isCustomer":"1","status":"4","updateTimestamp":"2020-05-08 20:24:48","createTimestamp":"2020-05-03 20:23:29","leadScoreWeighted":"23","leadScore":"26","isUnsubscribed":"0","leadStatus":"customer","persona":"","product_5e554b933fb5b":""}]},"error":null,"id":"5222020","callCount":"215","queryLimit":"50000"}
var resultSet = JSON.parse(JSON.stringify(obj));
}
but so what the point of the parse is to return an object from a string not from object.
But I do see the problem. 'Cannot connect to the server'
I found that this does seem to work know:
function test(){
var obj='{"result":{"lead":[{"id":"332","accountID":"","ownerID":"","companyName":"","title":"","firstName":"RYAN","lastName":"CAVANAUGH","street":"","city":"","country":"","state":"","zipcode":"","emailAddress":"email#here.com","website":"","phoneNumber":"","officePhoneNumber":"","phoneNumberExtension":"","mobilePhoneNumber":"","faxNumber":"","description":"","campaignID":"789934082","trackingID":"202003_5e6fa18a87853a69eb306910","industry":"","active":"1","isQualified":"1","isContact":"1","isCustomer":"1","status":"4","updateTimestamp":"2020-05-08 20:24:48","createTimestamp":"2020-05-03 20:23:29","leadScoreWeighted":"23","leadScore":"26","isUnsubscribed":"0","leadStatus":"customer","persona":"","product_5e554b933fb5b":""}]},"error":"","id":"5222020","callCount":"215","queryLimit":"50000"}';
var resultSet = JSON.parse(obj);
var end="is near";//I just put this here to have a place to stop with debugger running
}
I replaced all of the null s with "".
var cp = require("child_process");
var proc = cp.spawn("cmd");
proc.stdout.on("data", data => console.log("Data:", data.toString()));
proc.stdin.write("help\n");
proc.stdin.write("help\n");
In the above code snippet, how would you detect when the stream has finished writing for a specific command (i.e. when, if these commands were executed in the terminal, it would show a blinking cursor that you could enter text into)?
I have tried using listening to the event end, but this seems only to be fired when the process finishes.
There is no definitive way to know, unless the program you're executing has a well-defined output format (e.g. newline-delimited JSON, XML, etc.). Either way, you will have to perform some kind of parsing (and possible buffering) of the program output.
This can be solved by giving stdin a string which will be echoed back to you when the original command has been executed, where you know that the string executed have no unwanted effects and will probably not be outputted into stdout with the output of the command e.g. for the given example:
var cp = require("child_process");
var proc = cp.spawn("cmd");
proc.stdout.on("data", data => {
var str = data.toString();
console.log(str)
if(str.search("string to be detected") !== -1){
console.log("Command finished!");
}
});
proc.stdin.write("help\n");
proc.stdin.write("string to be detected\n");
Alternatively you could wait for some feature of the response which indicates the end of the command output such as a newline or a new prompt, as suggested by #mesdex and #user866762
i have googled around quite a bit but couldnt find anything that tells me how to get access to the stdin when executing a JS with jrunscript.
it seems not to be possible.
use case:
i want to write a little JS script that does a regex-replace for use on the cmd where i would want to pass in text data via piping, e.g.
>cat file | jrunsscript -f apply-regex.js
... and out comes the result (which i could pipe into a file, etc).
hence, i need to access to the stdin in order to read the what's piped unto jrunsscript.
side note: for this i usually use sed, but there are problems when the regex itself involves quotes in certain cases.
Not sure if this is on topic: http://docs.oracle.com/javase/7/docs/technotes/tools/share/jsdocs/GLOBALS.html
read(prompt, multiline)
Reads one or more lines from stdin after
printing a prompt
Based on your comment, I think you need to use arguments as shown: https://docs.oracle.com/javase/8/docs/technotes/tools/unix/jrunscript.html
Usage:
jrunscript -f test.js "Hello World"
Get the argument:
document.write(arguments[0]);
Upon more research, you may be able to read via Interactive mode:
jrunscript -f -
Interactive mode, read script from standard input. If this is used, this should be the last -f option
But still not clear if this is what you're looking for exactly.
the workaround thus far is to
pipe the input into a file
pass tempfile as arg to jsrunscript
use JS output as needed
Take a dip in Java.
in file JJS_SAYS_HI.js
//Java-y bit
var br = new java.io.BufferedReader(
new java.io.InputStreamReader(
java.lang.System.in,
java.nio.charset.StandardCharsets.UTF_8)
);
//JavaScript-y bit
var line = "";
while(br.ready()) {
line = br.readLine();
print("JJS says: ${line}");
};
Test the whole mess with a command like: echo Hi! | jjs -scripting JJS_SAYS_HI.js
Output is JJS says: Hi! (on Windows)
my question maybe quite straightforward or may have no solution. So, I ask you to kindly help me finding out how to access information of the stdout redirect to out.txt
node do-nothing.js < in.txt > out.txt
In particular I need to access the filename "out.txt" in the script do-nothing.js (i.e. accessing process.stdout.______)
Tips: I've already noticed that process.stdout._type become 'fs' instead of 'tty' while applying a redirection.
You can use this modulehttps://www.npmjs.com/package/yargs and then this module you named https://www.npmjs.com/package/fs in the other hand if you need something more complex you can use child process package to do what you need from args and execute a bash script you have for example
I got it. Here is the solution, if you launch the script as node stdout_redirect.js > out.txt && more out.txt you have the sought information in the d variable. Thanks for help and hope this help someone else.
var fs = require('fs');
fs.readdir(process.env.PWD,function(err, dir){
var stdout_ino = 0;
fs.stat('/dev/stdout',function(err,stats){
stdout_ino = stats.ino;
dir.forEach(function(d){
fs.stat(d,function(err,stats){
if (stats.ino === stdout_ino)
{
console.log(d);
}
});
});
});
});
I'm using HTA and in it I have a function that should run a command line with wshell.run , If I'm writing this line in Windows 'Run' util it is working fine, I want it to work also in the HTA with wshell.run.
The line is:
C:\xxxx\xxx\xxx.EXE aaa.psl abc
( The names are xxx just in here - not in the real code.. )
In the javascript code I'm using:
function runCmd()
{
wshShell.exec( "C:\xxxx\xxx\xxx.EXE aaa.psl abc" );
}
The error I got is in the xxx.EXE application says "couldn't open aaa.psl File not found".
Thanks,
Rotem
I'm surprised the xxx.EXE program is running at all. You need to escape those backslashes in the command:
wshShell.Exec( "C:\\xxxx\\xxx\\xxx.EXE aaa.psl abc" );
// ^-----^----^--- here
If you're doing the same thing in the aaa.psl filename, that's your problem.
If you're not passing a full path to the aaa.psl file, then most programs (not all) will expect it to be in the current directory, so you'll want to make sure you've set the current directory correctly (although using absolute paths may be a better option).
Here's an example, for instance, of telling Notepad to edit a file:
shell = WScript.CreateObject("WScript.Shell");
shell.Exec("c:\\windows\\system32\\notepad.exe c:\\temp\\temp.txt");
...or via the current directory:
shell = WScript.CreateObject("WScript.Shell");
shell.CurrentDirectory = "c:\\temp";
shell.Exec("c:\\windows\\system32\\notepad.exe temp.txt");
Okkkk T.J. is the man!! :)
I finnaly made it with your help by replacing exec to run:
This is the final (and working) code:
function runCmd()
{
wshShell.CurrentDirectory = "G:\\xxx\\xxx";
wshShell.run( "xxx.EXE xxx.psl abc" );
}