readFileSync is returning empty string - javascript

Reading a file using readFileSync is returning an empty string.
I am reading contents of a file using link which is retrieved from the database. It is working.
But I am unable to read the file using link which is stored in a variable.
My code is given below:
var result1={};
var filename="/home/rgukt/Project 2.0/CCMS/Admin/Challenges/"+(req.body.challenge_name)+"/"+(req.user.username)+"_expected_output"+(result[i].test_case_id)+".txt" ;
result1.output = fs.readFileSync(result[i].output,'utf8'); // working
result1.expected_output = fs.readFileSync(filename,'utf8'); // not working

Related

GetMethod.getResponseBodyAsString() to xml object

using Javascript HttpClient i am running a get method on WebService which works fine and then i store Response in a variable resp_va as shown below.
snip from code below.
var httpGetMethod = new GetMethod(url);
httpClient.executeMethod(httpGetMethod);
var statuscode = httpGetMethod.getStatusCode();
var resp_va = httpGetMethod.getResponseBodyAsString();
although output is in XML Format but to me Looks like it is string and therefore i am unable to parse it. Question is how can i convert "resp_va" in XML object before parsing it further?

Store file content in a table(html table tr td) jquery

I wanted to insert multiple files in database by uploading/adding the each file content to a table then after adding all files to the table i add the tables' value including the file content in a form data which i pass to ajax. I tried
var file = $('#file')[0].files[0];
//var file = document.getElementById('file').files[0];
var name = file.name;
var size = file.size;
var type = file.type;
var blob = new Blob([file], {
type: "application/octet-binary"
});
var filecontent = blob;
Here i used the value of filecontent to be passed to ajax but in php the condition if(!empty($_FILES) remaind false)
and on console.log filecontent looks like [object Blob] i want to do to achieve my primary goal which is to store to database i already asked a question about it. I can be found here this is the problem why i asked the first one.
My question is How to store file content to a variable in this case store the value of $('#file')[0].files[0] to file.
UPDATE
When i used the value of var file = $('#file')[0].files[0]; i still get the same error
UPDATE
Is it possible to store the input:file into table td or input or any other element and reuse that file if needed
To store content of a file into a variable you first need to read them , so you will have to use a FileReader.
For example readAsText() method Syntax:
instanceOfFileReader.readAsText(blob[, encoding]);
The readAsText method is used to read the contents of the specified
Blob or File. When the read operation is complete, the readyState is
changed to DONE, the loadend is triggered, and the result attribute
contains the contents of the file as a text string.
Hence you can store the result to your variable.

JavaScript JSON Combination and Data Anylish

I am trying to add values from multiple JSON responses that are saved in a .txt file. The .txt files has about 4000 entries. They are each the same format as follows:
{"id":"8f546dcf-b66a-4c53-b3d7-7290429483b8","price":"247.96000000","size":"0.03121005","product_id":"BTC-USD","side":"sell","stp":"dc"}
{"id":"0ec4b63a-b736-42af-a0aa-b4581bf12955","price":"247.90000000","size":"0.03910014","product_id":"BTC-USD","side":"sell","stp":"dc"}
{"id":"be403848-74dc-4494-8095-bd468777c958","price":"247.89000000","size":"0.04280854","product_id":"BTC-USD","side":"sell","stp":"dc"}
{"id":"ae2ae129-e850-4d8f-b945-55e65eb68a88","price":"247.83000000","size":"0.07941840","product_id":"BTC-USD","side":"sell","stp":"dc"}
{"id":"96194be4-40d8-446d-9f7e-ce72bc84af48","price":"247.63000000","size":"0.06225897","product_id":"BTC-USD","side":"sell","stp":"dc"}
I believe I need to combine the different JSON data sets before I can loop through them with a for loop and do the summing/analysis part (size:JSONSET1 + size:JSONSET2 + .....) but I'm not sure how I should call the .txt file in javascript and have it combine the multiple json parts. Suggestions??
Do you have any control over the file with the data set? If you do, you can make the input file one big JSON string.
Run this command in a terminal to add a comma to the end of every line:
sed -i 's/$/,/' yourFile.txt
Then edit the file with a text editor and put a [ at the beginning of the first line, and replace the last line's ending comma with a ].
Then after you read the file into a string, you can parse it like so:
var dataArray = JSON.parse(dataString);
And you could access the data like this:
console.log(dataArray[0].id);
This will print "8f546dcf-b66a-4c53-b3d7-7290429483b8" to the console
I would recommend using a .json file since you are working with JSON{Objects}.
I've made a demo of the file here and I've made a demo of the data analysis here.
Note: Demo file is hosted in a personal server, so it may not work later on.
Now the file isn't proper JSON Syntax, so it needs some parsing.
entries.json
{"id":"8f546dcf-b66a-4c53-b3d7-7290429483b8","price":"247.96000000","size":"0.03121005","product_id":"BTC-USD","side":"sell","stp":"dc"}
{"id":"0ec4b63a-b736-42af-a0aa-b4581bf12955","price":"247.90000000","size":"0.03910014","product_id":"BTC-USD","side":"sell","stp":"dc"}
(..)
JavaScript
Req = new XMLHttpRequest();
Req.onload = process_entries;
Req.open("get", "http://butler.tk/entries.json", true);
Req.send();
function process_entries() {
var response = Req.responseText;
var arrayed = "[" + response
.split("}")
.join("},")
.slice(0,-1)
+ "]";
var entries = JSON.parse(arrayed);
for (var i = 0, l = entries.length; i < l; i++) {
var entry = entries[i];
//entry.id,entry.size,etc.
}
}
We fetch the file with a XMLHttpRequest().
We parse the file so that it's valid JSON Syntax, the file will now look like this.
entries.json (parsed)
[
{"id":"8f546dcf-b66a-4c53-b3d7-7290429483b8","price":"247.96000000","size":"0.03121005","product_id":"BTC-USD","side":"sell","stp":"dc"},
{"id":"0ec4b63a-b736-42af-a0aa-b4581bf12955","price":"247.90000000","size":"0.03910014","product_id":"BTC-USD","side":"sell","stp":"dc"},
(..)
]
We parse the file into a JSON{Object}
We iterate through the array of objects and access their properties.
Note: If you have control over how the data is saved, you can save the data in the array format instead of parsing it.
Hope it helps! :)

Tasker Exported App Javascriptlet Error

I have created a Task to get the JSON result from 500px using HTTP GET and then used Javascriptlet code:
var arr=JSON.parse(global('HTTPD'));
var name=arr.photos[0].name;`
To parse the JSON result, everything is working fine when run from the TASKER.
But after exporting it as an APP, it gives error on JSON.parse():
Uncaught TypeError: undefined is not a function
I tried using .js file also. Can't figure out what am I missing?
I would use javascriptlet in following way.
url = "your url here.";
http = newXMLHttpRequest();
http.open("GET",url,false);
json = eval("("+http.responeText+")");
yourvariablename = json.var[0].name;
Also i do an variable set before the javascriptelet task.
for example. #variable set to ...
then set that #variable to some value within json.

JMeter modifying output to file from XML Stream

I'm attempting to write a JMeter script which after receiving and XML response from a server, extracts a string from it on the fly (drops the first part of the response) and writes it to a file.
Currently I use a Save Response Data to write to ChannelData_UAT_1 (filename). All good, it writes happily.
Then I add a BSF PreProcessor BEFORE it, and use javascript to try and extract the string. It's a bunch of XML tags, I want everything from "<Markets>" onwards.
I use:
function extract_markets(str)
{
marketIndex = str.indexOf("<Markets");
__log(marketIndex);
length = str.length;
marketString = str.substring(markeIndex, length-1);
return str;
}
vars.put('ChannelData_UAT_1', extract_markets(vars.get('ChannelData_UAT_1')));
As far as I can tell, ChannelData_UAT_1 is the variable the data is in. However this is only mentioned in the Save Response Data. But I can't do it afterwards otherwise it'll have already written to the file.
The current performance is for it to receive the response and write to the file. No filtering is done - as if my javascript didn't exist.
Anything small or obvious that I've missed? Suggestions?
I believe the issue stems from the fact that ChannelData_UAT_1 is not a variable and how Save Response Data works.
ChannelData_UAT_1 is the file name, not the content of the file.
You need to modify the contents of the "Response". You can replace the value of the page response with the value of your function.
I think the code would look something like this:
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jmeter.samplers.SampleResult;
prev.setResponseData(extract_markets(vars.get('ChannelData_UAT_1')));
Source:
http://www.javadocexamples.com/java_examples/org/apache/jmeter/samplers/SampleResult/

Categories

Resources