Using xml2js to parse an XML file, I need to retrieve the value of an attribute which contains a hyphen in its name
<item cdr-id="1234">
<name>some text</name>
</item>
At the point where I'm trying to retrieve cdr-id, I already have a variable item which points to the item element. I've verified it's pointing to the proper node with
console.log(item.name);
and that returns the expected value some text. But when I try
console.log(item.$.cdr-id);
I get the completely reasonable error ReferenceError: id is not defined (I'd have been more surprised if id wasn't treated as a separate token).
Likewise,
console.log(item.$."cdr-id");
gets the message SyntaxError: Unexpected string.
Throwing JSON.stringify around it
console.log( JSON.stringify(item.$));
reveals the original attribute name: {"cdr-id":"CDR0000040222"}
Not having hyphens in the name in the first place just kicks the problem down the road a bit. Failing that, it looks like providing a custom attribute name processor is the way to go, but that smacks of being "too clever" with potential for confusion if anyone ever has to update this code.
Is there a better way to do this?
do like
console.log(item.$["cdr-id"]);
Related
Im relatively new to javascript. I have an ID
btnradio_-1-1_4_-1
for an input button but it is being read as a Numeric Separator and giving me the error
Uncaught SyntaxError: Numeric separators are not allowed at the end of numeric literals
The ID is coming from a Model which reads all other data fine in the context. The format of the ID is as it is for tracking dynamic values on a razor/jquery web page, so its prefered that the ID format doesn't change.
How can you read it as just a string and not a Numeric Separator variable? I need the ID to call in a getElementByID().
I tried storing it in a separate string variable, as well as converting it to a string then using it, but I get the same error.
EDIT: My javascript is embedded in a razor section if that is causing any conflicts. I am unsure if its relevant or not. It is also alongside some jQuery.
#section scripts {
<script>
...
document.getElementById(#Model.ItemIDs[3]).className += "btn-check:active";
...
</script>
}
EDIT: I had the wrong ID pasted in the question. It has been corrected and should make more sense than the last one in the context
How do you extract default messages with variables or constants in the key or .get()
Example of message to extract
<p>
{
intl
.get(`${PREFIX}.personal-form.name-field`)
.defaultMessage('First name')
}
</p>
Running react-intl-universal-extract against the code above doesn't extract default message.
However when inserting a hard-coded value instead of ${PREFIX}, a message gets extracted.
Any ideas or different approaches to this work without any hard-coded values?
Since I couldn't find a direct solution to this issue.
A workaround that helped me is by using the hard-coded string value instead of variable/constant name inside the get method.
I'm triying to find the records that includes "SO -" or "NS - SO" or "SO –" or "SWAT" on THE "RESUMEN" field from a CSV file to asigne a new category (in this cases would be "Call Center"). So, I used "indexOf" funtion witch worked so well.
The problem comes when I change the data source (It is a CSV too), this gave me next error on that step:
"Caused by: org.mozilla.javascript.EcmaError: TypeError: Cannot call method "indexOf" of null (script#2)"
The objective is to assign a category by identifying the words on the source file
My code
if (RESUMEN.indexOf("SO -")!=-1 || RESUMEN.indexOf("NS - SO")!=-1 || RESUMEN.indexOf("SO –" )!=-1 || RESUMEN.indexOf("SWAT")!=-1)
{
var RESULTADO = "Call Center"
}
else RESULTADO = ""
I expect to assigne call center category like I got with the first file (I did not change nothing)
regards!
You're overcomplicating the issue.
Before the answer, remember something, there are several steps, and combinations of steps, that achieve an incredible number of transformations to make usable patterns, the last resort IS User defined Java Expression.
Seems like what you want to achieve is a Value Mapping, thou the difference from a direct value map in your case, is that the row you're testing must contain "SO -", and the other cases, somewhere in the text.
With this simple filter, you can transform your data that contains those informations as you desire, and on the "FALSE" side, treat it for errors.
This will expand your transformation a bit, but when you need to change something it will be easier than with a single step with a lot of code.
As another answer pointed out, you can achieve the same result with different steps, you don't need the javascript step.
But, if you want to go that route, you should first convert null values into, e.g., empty strings.
Simply add this to the beginning of your javascript code:
if (!RESUMEN){ RESUMEN = ''}
That'll convert nulls to empty strings and then indexOf returns correctly.
So basically I need to get a value out of a variable and insert it into a sequence.
I generally have no clue how to put this into words since I'm dutch but I hope you understand my question
var channelname = msg.channel.name;
"description": `${config.ticketlist.channelname.ticketmessage}`,
(Ofcourse highlights from the code)
But when I want to get the ticketmessage it doesn't work since channelname is not actually defined in my json file. It should be like ticket-001. But now its gonna search for channelname but not for the actual ticket name.
You can only use computed paths with bracket syntax. Use
"description": `${config.ticketlist[channelname].ticketmessage}`,
instead.
I've currently huge amount of data (500 mb each) which I'm using lodash and cheerio to parse and fetch parts of it.
Problem with new data is that it has some empty tags being incorrectly replaced.
Example:
<apple></apple>
gets replaced by
</apple>
I want to make sure that the previous formatting remains the same. Any regex that I can use to find these new empty tags and replace it with the old correct format?
You probably mean that <apple></apple> is replaced by <apple/> (not </apple>).
<apple></apple> and <apple/> are equivalent in XML, and no compliant XML process will treat them differently, so you should not care which is used in your document.
If you truly meant that <apple></apple> is replaced by </apple>, then you have a likely irreparably damaged file as you won't know whether any given end tag for apple should be associated with an empty or nonempty apple element.
For example, doing a string-level replace of "</apple>" to <apple></apple> for
<apple>one</apple>
would result in
<apple>one<apple></apple>
which would not be well-formed.