doesn't work:
console.log(obj.html_template); // outputs "myfile.html"
var html = fs.readFileSync(JSON.stringify(obj.html_template)); // file not found.
works:
console.log(obj.html_template); // "myfile.html"
var html = fs.readFileSync("myfile.html"); // Works.
I'm going crazy.
> JSON.stringify('myfile.html')
""myfile.html""
Your code is looking for the file "myfile.html" (note the superfluous quotes) in the filesystem. It doesn't exist.
Just look for it without stringification:
var html = fs.readFileSync(obj.html_template);
When you call JSON.stringify, it will convert all the Strings to the JSON format Strings, with surrounding double quotes. Quoting ECMAScript 5.1 Specification for JSON.stringify,
If Type(value) is String, then return the result of calling the abstract operation Quote with argument value.
And the Quote operation, is defined here, which basically surrounds the string with " and takes care of special characters in the String.
So JSON.stringify converts, a string, for example, abcd.txt to "abcd.txt", like this
console.log(JSON.stringify("abcd.txt"));
// "abcd.txt"
which is not equal to abcd.txt.
console.log(JSON.stringify("abcd.txt") == "abcd.txt");
// false
but equal to "abcd.txt".
console.log(JSON.stringify("abcd.txt") == '"abcd.txt"');
// true
So, your program searches for a file named "abcd.txt" instead of abcd.txt. That is why it is not able to find the file and fails.
To fix this problem, just drop the JSON.stringify and pass the string directly, like this
var html = fs.readFileSync(obj.html_template);
why are you using JSON.stringify in the first place? you should be able to just do
var html = fs.readFileSync(obj.html_template);
Related
I have a file called 'test.abcde.houses.csv' and I want to extract the substring 'abcde' which I will use in my next processor group to query the database.
Currently, I am using the updateAttribute Processor group to try to extract the substring.
This is the code I am using in the value section.
var userPattern = java.util.regex.Pattern.compile('(.+?)\.[0-9]{8}-[0-9]{7,9}\..+');
var userMatcher = userPattern.matcher(fileName);
var matchExists = userMatcher.matches();
var user;
var userRemove;
if (matchExists) {
user = userMatcher.group(1);
userRemove = user + ".";
}
else {
throw 'Unable to parse username from file metadata.';
}
Question:
Is this the right way to extract a substring from a flow file name in NIFI?
Am I using the right processor group?
Does this code work with Nifi?
You need to use NiFi Expression Language, a kind of NiFi's own scripting feature which provides the ability to reference attributes, compare them to other values, and manipulate their values. Please refer to this official documentation.
UpdateAttribute processor is used to update/derive new/delete attributes. So you need to use the Expression Language inside UpdateAttribute to manipulate attributes.
Example:
test.abcde.houses.csv - this is your filename and if you want to extract abcde string from filename then you can use getDelimitedField function (Expression Language string function) like below. If the expression did not evaluate, then user attribute will be having empty/null value.
Property: user (if already present then update/assign value, otherwise create new attribute)
Value: ${filename:getDelimitedField(2, '.')} (abcde is at second index/position in filename attribute value)
Expression Language has Boolean, Conditional, String Manipulation, etc. functions, so you can easily replicate your JS logic into UpdateAttribute to derive desired attribute value.
I am trying to create a custom javascript variable in GTM that returns part of a javascript variable that already exists.
Variable that already exists: window.ShopifyAnalytics.meta.product.variants.0.name
returns this: "Bamboo Basic String - Schwarz - S"
However I want to code a custom javascript variable to just return the Schwarz part, is this possible? If so what is the code that I would need?
Please can someone let me know what code to put into GTM to create this variable?
TIA
If all names are pretty much the same you could use split to get that part of string and then remove whitespaces. It would look like this:
window.ShopifyAnalytics.meta.product.variants.0.name.split('-')[1].replace(/
/g,'');
If the already existing variable is always structured the same way you could do something like this:
let variable = window.ShopifyAnalytics.meta.product.variants.0.name.split('-')
Then by calling varaible[1] you get the 'Schwartz' part of the variable.
If you want a return value you can use a function like the following and call it wherever you want.
Simply make sure to pass the correct argument content
// Declaring a function getColor that returns the second element in the list,
// trimmed (without spaces before and after)
const getColor = (content) => {
return content.split('-')[1].trim();
}
const test = "Bamboo Basic String - Schwarz - S";
console.log(getColor(test));
//console.log(getColor(window.ShopifyAnalytics.meta.product.variants.0.name));
You could split the string on the hypens (-) like this:
const productName = window.ShopifyAnalytics.meta.product.variants.0.name;
const part = productName.split(' - ')[1];
Assuming you have a consistent format, and you always want the second part after that hyphen.
split will separate parts of a string into an array where it finds a match for the argument. The first index [0] will be the product name, the second [1] will be the part you're looking for.
This could cause issues if you have a product name with a - in it too though so use with care!
If it needs to be an anonymous function for GTM, you could try the following (though I'm not a GTM expert):
function () {
const productName = window.ShopifyAnalytics.meta.product.variants.0.name;
return productName.split(' - ')[1] || 'Unknown';
}
I get the following when retrieving it.
var data = {"distinct_id"%3A "2222222222222"%2C"%24initial_referrer"%3A "%24direct"%2C"%24initial_referring_domain"%3A "%24direct"}
If I check for typeof data I get a String back.
However, when I try to make a proper object out of it by replacing "%3A" with ":" etc the above object does not replace all occurrences but only the first.
data = data.replace(/\%3A/g,":") only replaces the first "%3A".
How can I make a proper object out of this with distinct_id, $initial_referrer as well as we $initial_referring_domain ?
Testing your code proves that your replace usage is actually okay, it indeed replaces all occurrences of %3A:
var data = '{"distinct_id"%3A "2222222222222"%2C"%24initial_referrer"%3A "%24direct"%2C"%24initial_referring_domain"%3A "%24direct"}';
data = data.replace(/\%3A/g, ":");
alert(data);
However, regular expressions is not correct approach here, as you also have other encoded entities. Use decodeURIComponent function instead:
var data = '{"distinct_id"%3A "2222222222222"%2C"%24initial_referrer"%3A "%24direct"%2C"%24initial_referring_domain"%3A "%24direct"}';
data = decodeURIComponent(data);
alert(data);
I have a (simple, I guess) problem with quotes, single quotes, double quotes.
I have a JS that sends data to a php file, which responds sending some data back with json. In the code below, row.Dispon is part of the response (and is working OK). But I want to "echo" row.Element inside getElementById with no success. I've tried "+row.Element+", or "'+row.Element+'". What I'm doing wrong?
if (row.Dispon=="ImageReload") {
var text='Image changed';
document.getElementById(+row.Element+).value="due";
}
Considering your code snippet only, this should do the job for that specific problem:
if (row.Dispon == "ImageReload") {
var text = 'Image changed';
document.getElementById(row.Element).value = "due";
}
You would need quotes (or double quotes) and + operators if you were trying to build a string. See this example:
var id = 42;
document.getElementById('myId' + id).value = 'something';
Assuming that row.Element contains a string already, you can directly pass it to getElementById().
Some advice here:
Read more about functions on MDN
Read more about Document.getElementById on MDN
Consider using Document.querySelector
I have returned object from signature device and when i make quick watch on it, it told me that its an array of long and when i pass it to web method in my code behind (vb.net) it gives me nothing.
i need some help..
note: i'm using an activeX to capture the signature from the device.
this is javascript code :
function OnSave() {
var sign = document.FORM1.SigPlus1.SignatureString;
PageMethods.Save(sign);
}
this is my webmethod:
<WebMethod()> _
Public Shared Function Save(ByVal obj As Object) As String
Dim obj1 As New PFSIGNATURELib.SigniShellSignature
obj1.SignatureMime = obj
obj1.SaveBitmapToFile(System.AppDomain.CurrentDomain.BaseDirectory() & "\sign1.bmp", 200, 200)
Return "\sign1.bmp"
End Function
I don't know much about ASP.Net, but it seems like the PageMethods.Save function can't handle an array of long. Another possibility is that the sign variable is null in the javascript code.
Try adding
alert(sign);
in the middle your Javascript function, or better yet, install firebug and do
console.log(sign);
instead. This way you'll make sure the sign var actually contains what you think it does.
If it indeed contains an array of numbers (javascript doesn't have a long type), maybe you need to convert it to something else before calling the PageMethods.Save function.
For example, this javascript snippet will convert sign into a space-separated string of numbers:
s = ""
for (i in sign) {
s += sign[i] + " ";
}
sign = s
If you manage to pass this string to your webmethod, you can use some string parsing to get back the original array.