Prevent Javascript from replacing beginning of string with NaN? - javascript

I am work on some OData query calls to Microsoft CRM and need my query in a very specific format.I am passing parameters to a function which then adds the URL to my query. What I am passing my retrieve function is as follows:
webAPI.REST.retrieveEntity(
"EntityDefinition",
id,
+ "/Attributes(LogicalName='" + logicalAttribute + "')"
+ "/Microsoft.Dynamics.CRM.PicklistAttributeMetadata"
+ "?$select=LogicalName&$expand=OptionSet"
, null)
In debugging my parameter with the query options is:
"NaNmylogicalattribute')/Microsoft.Dynamics.CRM.PicklistAttributeMetadata?$select=LogicalName&$expand=OptionSet"
As you can see, my "/Attributes(LogicalName=" was replaced with "NaN". How do I prevent this from occurring?

Here is the problem:
id,
+ "/Attributes(LogicalName='" + logicalAttribute + "')"
Since you aren't starting with a String, the leading + is coercing your String into a Number (it isn't a Number, hence NaN). Just remove that first leading + and it will work:
id,
"/Attributes(LogicalName='" + logicalAttribute + "')"

Related

Javascript String to Float conversion inside string building

I am preparing database insert query in Snowflake Stored Procedure.
My expected output is 2.0
console.log(`INSERT INTO MY_SCHEMA.MONITORING (COUNT, MESSAGE) VALUES (` + parseFloat('1.0') + 1 + `, 'DUM')`);
console.log(parseFloat('1.0') + 1);
Output of a 1st Line:
INSERT INTO MY_SCHEMA.MONITORING (COUNT, MESSAGE) VALUES (11, 'DUM')
Output of a 2nd Line:
2
What is happening at a first line and why its returning 11 instead of 2 ?
Remember that you are concatenating a string, so if you want to make an addition before the concat, you should use a parenthesis (parseFloat('1.0') + 1). Use the toFixed() function to indicate the decimals after comma or dot:
console.log(`INSERT INTO MY_SCHEMA.MONITORING (COUNT, MESSAGE) VALUES (` + (parseFloat('1.0') + 1).toFixed(1) + `, 'DUM')`);
console.log((parseFloat('1.0') + 1).toFixed(1));
You're converting to float and then immediately concatenating into the string. Instead, you can make use of your template literal.
`INSERT INTO MY_SCHEMA.MONITORING (COUNT, MESSAGE) VALUES (${parseFloat('1.0') + 1}, 'DUM')`

Why is the following way to assign a value to a JSON array not working?

I have this code:
compareList[productName] = productID + ',' + productHref;
console.log(productName + ' ' + productID + ' ' + productHref + ' ' + compareList.length);
Which logs into this (I have removed the link):
Acer Iconia B1-790 [NT.LDFEE.002] 112576 link removed for confidentiality 0
As you can see, all three variables are valid strings, but the json object still fails to assign (compareList.length logs as 0). I've been thinking and thinking but I simply can't figure it out. Any help is appreciated.
Maybe this version of adding and checking array length can be useful to you?
var compareList=[]
var productName = {productID:'saban',productHref:'http://saulic.com'};
compareList.push(productName);
console.log(compareList.length);

Send an array of objects from JS/TS (Angular 2) to WCF?

i have to send an array of objects like this
[{"Cod":"1"},{"Cod":"5"}]
to my C# WCF Service which i used until today without problems with wsHttpBinding (so no JSON using here, only XML).
The request, POST, contains this param:
let param = "<cod>" + data + "</cod>";
My problem is on WCF because i can't find a way to get that array of objects from the parameter of the method:
public string GetArrayOfObjects(CompositeType[] cod)
{//implementation not important because i can't enter here...}
where CompositeType is:
[DataContract]
public class CompositeType
{
string cod;
[DataMember]
public string Cod
{
get { return cod; }
set { cod = value; }
}
}
I have tried so many things like changing the parameter to:
Object[] cod
List<string> cod
...
Everytime i get this error (translating):
Exception generated by the formatter in an attempt to deserialize the message: Error trying to deserialize the parameter http://tempuri.org/:cod. InnerException: 'Error at line 1 position 341. Expected status 'Element' .. Found 'Text' with '' name space ''. '.
...
This is the post message that i'm sending:
"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<s:Envelope xmlns:a=\"http://www.w3.org/2005/08/addressing\" xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\">" +
"<s:Header>" +
"<a:Action s:mustUnderstand=\"1\">http://tempuri.org/IService1/" + GetArrayOfObjects + "</a:Action>" +
"</s:Header>" +
"<s:Body>" +
"<" + GetArrayOfObjects + " xmlns=\"http://tempuri.org/\">" +
param +
"</" + GetArrayOfObjects + ">" +
"</s:Body>" +
"</s:Envelope>";
where param is defined before. The JSON.stringify of data inside param gives the first example of this question ([{"Cod":"1"},{"Cod":"5"}]). This post message was always fine with single value params.
Some sites show a solution with a json deserializer on wcf, but i'm using XML...it's not an option right now to re-implement the project in json.
I tried for one day but i didn't find a solution, i'd love to hear some solutions from you! Thanks to all.

Setting up a javascript url

I simply want to set up an ajaxurl in javascript to pass to my controller.
When trying to setup the url, it evaluates to 0 during the below attempts:
Note that attrs.attachmentType & attrs.attachmentId have values associated with them.
var hoser = attrs.attachmentType & attrs.attachmentId;
ajaxUrl = root + 'FileUpload/upload?' & 'Type=' & attrs.attachmentType & 'ID=' & attrs.attachmentId;
If I just include the following, a proper value is seen inside the hoser variable.
var hoser = attrs.attachmentType;
What am I doing wrong where the url will not come out correctly?
You are using & symbols, not adding them to the string. By using these symbols, javascript is interpreting the right hand side of your statement to be a comparison and outputting the answer it thinks you want - in this case, a boolean representation of false: 0
Change your & symbols to strings. Like so:
ajaxUrl = root + 'FileUpload/upload?' + '&' + 'Type=' + '&' + attrs.attachmentType + '&' + 'ID=' + '&' + attrs.attachmentId;

pass arguments into javascript method from code behind

I have a javascript method that checks the following condition
method(selection1,selection2)
{
if(selection1=="yes")
{
//Do something
}
if(selection2=="yes")
{
//Do something
}
}
now i pass arguments from code behind like this
ClientScript.RegisterStartupScript(GetType(), "id", "method('" + selection1 + "," + "'" + selection2 + "')", true);
Here selection is a string variable
string selection1="Yes"
But the desired functionality doesn't work out. I know the javascript is correct because when i use hardcoded arguments then the javascript runs.
Kindly help. Thanks
Call it this way:
Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", javascript:method('"+selection1+"','"+selection2+"')", true);
This will call the function and send the params as well, just be sure about the case that use in the string.
Your code missing ' ending quotes for first string argument.
Use like this
"method('" + selection1 + "', '" + selection2 + "')"
For comparison to be success the string must be exactly equal. selection1 value should be "yes" for the condition to be success

Categories