Just added Flow types to a project I'm working on and progressively adding types until I got to this error:
Cannot call JSON.parse with localStorage.getItem(...) bound to text because null or undefined [1] is incompatible with
string [2]
This comes from a expression:
const myVar = JSON.parse(localStorage.getItem('itemName'))
I understand why I get this error (except maybe the "bound to text" part), but couldn't find a way around it. I'd appreciate any help here!
So, the function localStorage.getItem can return null values and flow wants you to tackle them before parsing it. As JSON.parse only takes a string, you can do the following:
localStorage.getItem("key") || '{}'
So, if it returns null. The empty object string is chosen, which JSON.parse can parse into an empty object.
Prefer using 'null' than '{}' as it parses to empty object
JSON.parse(localStorage.getItem("key") || 'null') // null
JSON.parse(localStorage.getItem("key") || '{}') // {} - empty object
Related
I have the following fragment of code
with GNAT.Command_Line; use GNAT.Command_Line;
with GNAT.Strings; use GNAT.Strings;
....
Define_Switch
(Config => Config, Output => File_Name'Access,
Long_Switch => "--file=", Switch => "-f=",
Help => "File with Composition");
....
Getopt
After parsing command line via Getopt I have access object that points to actual file name
I would like to copy this name to Ada.String.Fixed string that definded as
File_Name : String(1 .. 256);
I can print to console data from File_Name'Access as
Put_Line(File_Name.all);
I think I should provide something like copy operation then free access object.
How can I do it?
Thanks.
Alex
I guess File_Name in the code snippet defined as 'aliased GNAT.Strings.String_Access'. This is a "fat pointer" to the string object. "Fat" means it is not an address only, it is range of indices of the string. C-style Nil terminator is not used in Ada, and Nil is valid character.
You can copy data inside this string object into the another standard String object playing with indexes computations, but usually you must not do this: there is no Nil terminator, you will need to pass length of the actual data; destination string object may be smaller than necessary, and data will be truncated or exception raised; etc.
There are two right ways to do this. First one is to declare unconstrained string object and assign value to it.
declare
Fixed_File_Name : String := File_Name.all;
begin
Free (File_Name);
or use variable length string (bounded or unbounded):
declare
Unbounded_File_Name : Ada.Strings.Unbounded.Unbounded_String;
begin
Unbounded_File_Name :=
Ada.Strings.Unbounded.To_Unbounded_String (File_Name.all);
Free (File_Name.all);
Use of fixed string has important restriction: string object must be initialized exactly at the point of declaration of the object, and available only inside corresponding block/subprogram. Use of variable length string allows to declare string object outside of the scope of particular block/subprogram.
I am new to XQuery. Please guide me to solve the issue below I want to return the null value as a string, if the below expression does not give any value.
Currently, the output doesn't show the 'name' field itself. I want to have a name with null. for eg-
if (IsNull(expression),null,expression)
$output.dataAccessResponse[1]/*:row/*:name/text()
You could use the fn:exists() function to test whether or not there is a text() node.
exists($output.dataAccessResponse[1]/:row/:name/text())
You could also use the fn:boolean() function to test the effective boolean value of the node.
boolean($output.dataAccessResponse[1]/:row/:name/text())
If you wanted to test whether or not there was a significant value i.e. something other than whitespace, you can fn:normalize-space() in a predicate, to ensure that only text() nodes that have meaningful text are selected, and then test fn:exists().
exists($output.dataAccessResponse[1]/:row/:name/text()[normalize-space()])
XQuery doesn't have null, so if you are asking what to return to indicate null, then you would want to return an empty sequence () instead of null.
So, you could execute something like this:
let $name := $output.dataAccessResponse[1]/:row/:name/text()
return
if (fn:exists($name))
then $name
else ()
But at that point, it's really the same as just attempting to select the text() with that XPath and it will either return the text() node or an empty sequence:
$output.dataAccessResponse[1]/:row/:name/text()
I'm not finding a function to test for the existence of a value in the ISML template code. There's 'isDefined' but not 'isNull'.
isDefined returns true on null values:
<isset name="woot" value="" scope="request">
<isif condition="#isDefined(woot)#">
<h1>woot</h1>
</isif>
For now I'm using:
<isif condition="#woot EQ null#">
or
<isif condition="#woot EQ ''#">
I don't know if this will work for Boolean values.
isDefined is how you would check for a null value. In AbstractTemplate you have the method isDefined(Object anObject) that is called. Checkout the compiled jsp and java versions of your isml template.
In AbstractTemplate
public Boolean isDefined(Object anObject){
...
return anObject != null ? Boolean.TRUE : Boolean.FALSE;
}
The code in your example is a bit misleading, it doesn't actually test for a null reference. Bear with me.
First statement :
<isset name="woot" value="" scope="request">
Compiles to :
Object temp_obj = ("");
getPipelineDictionary().put("woot", temp_obj);
This just sets the woot variable to an empty string. If you add the following scriptlet to your isml you will see that it really isn't a null.
Disclaimer: don't use scriptlets in production code, this is only for demonstrating a point
<%
Object woot = getPipelineDictionary().get("woot");
out.print(woot == null); //print false
%>
Second line:
<isif condition="#isDefined(woot)#">
Evaluates if the variable exist and it does. It has an empty string as a value, not null like you might think.
So what happens here then?
<isif condition="#woot EQ null#">
Looking at the compiled version:
context.getFormattedValue(getObject("woot"),null).equals(context.getFormattedValue(getObject("null"),null))
The context.getFormattedValue(getObject("null"),null) is the important bit here. It tries to retreive the variable called null, it doesnt exist so returns null. The getFormattedValue method then returns an empty string for the null argument (see TemplateExecutionConfig::getFormattedValue). The whole statement then evals to true. Not because woot is null, but because you are comparing it to a variable that doesnt exist, so you are inadvertently evaluating two empty strings. This behaviour is consistent with the EQ operator because it is use to compare strings.
You would get the same result if u would use this statement too.
<isif condition="#woot EQ iDontExistButImAlsoNotNull#"> //true
The third statement compares the woot variable to an empty string value, which returns true.
<isif condition="#woot EQ ''#">
Compiled version:
context.getFormattedValue(getObject("woot"),null).equals(context.getFormattedValue("",null))
So the real problem is that woot doesn't have the literal value null. See the following code:
<isset name="foo" value="#IDontExitPrettySureAboutThat#" scope="request">
<%
Object foo = getPipelineDictionary().get("foo");
out.print("foo is null? ");
out.print(foo == null);
//prints : foo is null? true
%>
<isif condition="#isDefined(foo)#">
<h1>foo1</h1> //is never printed
</isif>
I'm abusing the fact that IDontExitPrettySureAboutThat doesn't exist to set a null value to foo. isDefined then starts to work like you would expect. That is until someone initializes my variable to something other than null.
I wouldn't advocate that you use this method, however. I think the best advice is not to use null to represent a missing value or invalid state.
This thread goes into some details on this topic.
' >
System.InvalidCastException: Conversion from type 'TimeSpan' to type 'String' is not valid
' >
I have a gridview , i want that the column"duréeCalculée" take like this value "text='<%# (TimeSpan.Parse(Eval("heure_retour") - Eval("heure_depart"))).ToString()%>'"
but when my page generate I got this problem(Conversion from type 'TimeSpan' to type 'String' is not valid)
Can U help Me?
I'm uncertain with this little information that anyone will be able to help you. It would seem that TimeSpan.Spare() is returning a structure of type TimeSpan. When you try to use .ToString(), your compiler isn't figuring out what to do because .ToString() isn't recognised as a conversion method for types TimeSpan.
Most likely what you need to do is get the element TimeSpan that you want and then parse it. Something along the lines of
double Element1 = TimeSpan.Element1;
char str[64];
sprintf(str,"%f",Element1);
Where Element1 is the name of the element you wish to retrieve.
I'm new to JavaScript. In nashorn 1.8.0_11 I see the behavior below. Note print(x) works fine yet evaluating x causes a crash. May I consider this a bug? If so, is it a known bug?
jjs> var x = Object.create(null);
jjs> print(x);
<shell>:1 TypeError: Cannot get default string value
jjs> x;
Exception in thread "main" ECMAScript Exception: TypeError: Cannot get default string value
at jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:56)
at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:212)
at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:184)
at jdk.nashorn.internal.objects.Global.getDefaultValue(Global.java:592)
at jdk.nashorn.internal.runtime.ScriptObject.getDefaultValue(ScriptObject.java:1257)
at jdk.nashorn.internal.runtime.JSType.toPrimitive(JSType.java:256)
at jdk.nashorn.internal.runtime.JSType.toPrimitive(JSType.java:252)
at jdk.nashorn.internal.runtime.JSType.toStringImpl(JSType.java:993)
at jdk.nashorn.internal.runtime.JSType.toString(JSType.java:326)
at jdk.nashorn.tools.Shell.readEvalPrint(Shell.java:449)
at jdk.nashorn.tools.Shell.run(Shell.java:155)
at jdk.nashorn.tools.Shell.main(Shell.java:130)
at jdk.nashorn.tools.Shell.main(Shell.java:109)
This is as expected. When you evaluate expressions interactively with "jjs" shell tool, it converts evaluated expression result as String to print the same. Also, "print" function calls toString on object to print string representation of it to console. With Object.create(null), you are creating an object whose prototype is null (and hence does not inherit Object.prototype.toString). Also, your object does not have "toString" property with function typed value and hence the TypeError. Please note that you can similar behaviour with v8 shell as well.
V8 version 3.25.15 [sample shell]
var x = Object.create(null)
x;
print(x)