How can I rectify " Expected String instead saw "" " using jslint? - jslint

I've got a regular expression:
return (str+'').replace(/^(.)|\s(.)/g, function ( $1 ) { return $1.toUpperCase ( ); } );
I get following jslint error:
Expected String instead saw ""
How can I rectify this error?

It wants you to use
String(str)
isntead of
(str+'')
Invoking the String function as a "cast" is a slightly cleaner way to convert something to a string from some other type.

Use toString();
(new Date()).getTime()+""; instead (new Date()).getTime().toString();

Related

Trying to pass parameter as binding variable in snowflake statement

Below is my stored procedure, I'm not sure as to why it keeps throwing an error. The error I get is
SQL compilation error: syntax error line XX at position XX unexpected '?'.
I have followed the documentation here but it does not seem to work for me.
This is what I have:
CREATE OR REPLACE PROCEDURE spExample(INPUT_TABLE VARCHAR)
RETURNS VARCHAR
LANGUAGE JAVASCRIPT
AS
$$
result = "";
try {
var sql_cmd = "SELECT * FROM ?;";
var sql_stmt = snowflake.createStatement({sqlText: sql_cmd, binds:[INPUT_TABLE]});
sql_stmt.execute();
} catch(err) {
result += "Message: " + err.message;
}
return result;
$$;
Have I made a mistake somewhere?
Above answers subject your code to sql injection attack. And of course you can bind a table name to a variable in snowflake.
Do
var sql_cmd = "SELECT * FROM IDENTIFIER('?');";
It seems I had the exact same misunderstanding as the OP. It was good to find this answer.
In any case, it's a lot more flexible & more readable to use JavaScript template literals using backticks (instead of using single quotes or double quotes). They allow you to use expression interpolation in the format of
`Some text here. ${expression} Some more text here.`
Just fill in with your variable or variables (or expression).
Here is what I tried and it executed perfectly:
CREATE OR REPLACE PROCEDURE spExample(INPUT_TABLE VARCHAR)
RETURNS VARCHAR
LANGUAGE JAVASCRIPT
AS
$$
result = "";
try {
var sql_cmd = "SELECT * FROM IDENTIFIER(?);";
var sql_stmt = snowflake.createStatement({sqlText: sql_cmd, binds:[INPUT_TABLE]});
sql_stmt.execute();
} catch(err) {
result += "Message: " + err.message;
}
return result;
$$;
Call spExample('PAIDBILLS');
For me, this worked, first Quotes then brackets. I think its different for different type of query , other answers worked for me for select but not for SHOW
var sql_cmd = "SHOW WAREHOUSES like '(?)';"
CREATE OR REPLACE PROCEDURE spExample(INPUT_TABLE VARCHAR)
RETURNS varchar
LANGUAGE JAVASCRIPT
execute as owner
AS
$$
result = "";
try {
var sql_cmd = "SHOW WAREHOUSES like '(?)';";
var sql_stmt = snowflake.execute({sqlText: sql_cmd, binds:[INPUT_TABLE]});
} catch(err) {
result += "Message: " + err.message;
}
return result;
$$;
Call spExample('WREHOUSE NAME');
actually yes.
Bind variable is just that, a variable. So you can do a
SELECT * FRMO MY_TABLE WHERE MY_COLUMN=?
but you can't use bind to substitute for commands or column or table names. You can however use simple JS concatenation, like
var sql_cmd = "SELECT * FROM "+INPUT_TABLE;

Method doesn't work when I use ? after int

I have this Action method :
public ActionResult Index(int? mid)
{
IList<SubGroup> SubGroupLit = (mid != null) ? _subGroupService.GetAllWithGroupId(mid) : _subGroupService.GetAll();
return View(SubGroupLit);
}
I getting this ERROR:
he best overloaded method match for 'ServiceLayer.Interfaces.ISubGroupService.GetAllWithGroupId(int)' has some invalid arguments
But when I remove the ? after the int (in Index parameters), it works fine .
What is wrong?
The ? means it's nullable. Your GetAllWithGroupId method, however, only accepts int. If mid just happens to come through as null, then it can't be passed into that method because null is not an int. Hence the error. What you'll have to do is make mid just an int, which, as you noted already, works. Or you'll need to check mid has a value and then pass the value into the method. It looks like you were already attempting to do this with the ternary, but you made a few mistakes. The following code should work:
IList<SubGroup> SubGroupLit = mid.HasValue ? _subGroupService.GetAllWithGroupId(mid.Value) : _subGroupService.GetAll();

Getting dynamic values from neo4j using Spring data

public interface WayPointRepository extends GraphRepository, NamedIndexRepository, RelationshipOperationsRepository {
#Query( value = "start point=node:waypoints(\"name:{name1}\") return point", elementClass=WayPoint.class, type=QueryType.Cypher )
public List getWayPointByName(#Param("name1") String name);
}
i have a neo4j database with some points stored in it with index "waypoints", i want to get
some points dynamically after passing some points. for this i have created placeholder {name1},
but on calling the getWayPointByName with dynamic param gives me
nested exception is java.lang.RuntimeException: org.apache.lucene.queryParser.ParseException: Cannot parse 'name:{name1}': Encountered " "}" "} "" at line 1, column 11.
Was expecting one of:
"TO" ...
...
...
] with root cause
org.apache.lucene.queryParser.ParseException: Encountered " "}" "} "" at line 1, column 11.
Was expecting one of:
"TO" ...
...
...
at org.apache.lucene.queryParser.QueryParser.generateParseException(QueryParser.java:1818)
at org.apache.lucene.queryParser.QueryParser.jj_consume_token(QueryParser.java:1700)
at org.apache.lucene.queryParser.QueryParser.Term(QueryParser.java:1507)
at org.apache.lucene.queryParser.QueryParser.Clause(QueryParser.java:1309)
at org.apache.lucene.queryParser.QueryParser.Query(QueryParser.java:1237)
at org.apache.lucene.queryParser.QueryParser.TopLevelQuery(QueryParser.java:1226)
at org.apache.lucene.queryParser.QueryParser.parse(QueryParser.java:206)
exception
Use
start point=node:waypoints(name={name1}) return point
Within strings there is no parameter substitution happening.

Render multiple spaces in var

I am trying to add multiple spaces in my var but it get's cut down to one space or it renders out & nbsp; as is. I have tried using & nbsp; and %20 any one have any other ideas?
ViewBag.Subheading = "Bringing to light";
I want it to look like this
Bringing to light
ViewBag.Subheading = "Bringing to light".Replace(" ", " ");
And
#Html.Raw(ViewBag.Subheading)
Or you could do something like:
public static MvcHtmlString DisplayAndRetainSpaces(this HtmlHelper html, string value)
{
return MvcHtmlString.Create(value.Replace(" ", " "));
}
Then call it like:
#Html.DisplayAndRetainSpaces(ViewBag.Subheading)
Use the entity for that: for each space you want.
EDIT: If you already tried and didn't work, there's an helper for outputing html, which should work with the entity:
#MvcHtmlString.Create(" ");

Using __arglist with a varying set of named parameters

in my application I have 2 layers. The first layer is a C legacy exposing cdecl functions that use the "..." syntax for a varying parameter list. The only way I found to call these functions from my .Net layer (the second one) is using the DllImport technics. For example the C function below:
int myFunc(char* name, ...);
looks like that in C#:
[DllImport("MyDll.dll"),
CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.Cdecl]
int myFunc(string name, __arglist);
My problem is that sometimes I want to call this function with 2 extra parameters but if one of them is NULL it won't be included in the argument list (my legacy code fails on NULL values). For example I want this call:
int foo(string name, string a, string b)
{
myFunc(name, __arglist(a, b));
}
{
foo("john", "arg1", null);
}
to be interpreted by C as
myFunc("john", "arg1");
Unfortunately doing something like that:
int foo(string name, string a, string b)
{
List<string> args = new List<string>();
if(a != null) args.Add(a);
if(b != null) args.Add(b);
myFunc(name, __arglist(args));
}
{
foo("john", "arg1", null);
}
is interpreted by C like that:
myFunc(name, args);
and not:
myFunc(name, args[0]);
Does anybody have any idea?
How does the C function know which one is the last parameter? It cannot know a priori how many parameters there are. It needs additional information. One common way for functions get the information they need is by parsing the included string parameter to count format specifiers, as in printf. In that case, if the format string only indicates that there is one extra parameter, then the function doesn't know the difference between a call that really had just one extra parameter and a call that had two or a call that had 20. The function should have the self-discipline to only read one parameter, since that's all the format string said there was. Reading more would lead to undefined behavior.
If what I've described is not the way your function works, then there's not much you can do on the calling end to solve it. But if it is how your function works, then there's nothing to do on the calling end, because there's no problem.
Another option, since you indicate that your "legacy code fails on null values," is to fix your legacy code so it doesn't fail anymore.
A third option is to simply write all four possibilities:
if (a != null) {
if (b != null)
return myFunc(name, a, b);
else
return myFunc(name, a);
} else {
if (b != null)
return myFunc(names, b);
else
return myFunc(names);
}
More than two optional parameters, though, and the code starts getting unwieldy.
Try converting your System.List ToArray() before wrapping it in __arglist
myFunc(name, __arglist(args.ToArray()));

Resources