I am trying to use IF statement in $http.post {} section.
I have written the code as below:
[controller.js]
$http.post("../crud/projects_update.php",{
step_number : $scope.step_number,
//step_one start
if(step_number == 1){ // This is where I get an error.
project_id : $scope.project_data.project_id,
project_title : $scope.project_data.project_title
}
})
.then(function(response){
// do something here
});
However, I get an error on (step_number == 1) part with red underline on '==' part.
I thought it would be working in a simple IF statement form.
Perhaps, I am not using the IF statement in correct comparison syntax..
I have no idea why it is giving me a red line on the '=='.
Does anyone know what could possibly wrong? Please advise me how to fix this error.
Thank you so much in advance!!!
The problem here is that the second parameter for the $http.post function is an object, and you can't use if statements when you are creating an object literal.
That's not an AngularJS thing - it's flat out invalid JavaScript.
There are many possible ways you could do what you're trying to do.
One possible solution is:
$http.post("../crud/projects_update.php",{
step_number : $scope.step_number,
project_id : $scope.step_number === 1 ? $scope.project_data.project_id : undefined,
project_title : $scope.step_number === 1 ? $scope.project_data.project_title : undefined
})
Related
Javascript array value is undefined ... how do I test for that
and
How to check a not-defined variable in JavaScript
these are wrong as far as I'm concerned :
I ONLY get :
when trying to :
console.log(!fromToParameters[7].value.firstInput);
console.log(!!fromToParameters[7].value.firstInput);
console.log(fromToParameters[7].value.firstInput === undefined);
console.log(typeof fromToParameters[7].value.firstInput == 'undefined');
console.log(fromToParameters[7].value.firstInput !== undefined);
console.log(typeof fromToParameters[7].value.firstInput != 'undefined');
but this (the entry exists) works fine :
console.log(!fromToParameters[0].value.firstInput);
console.log(!!fromToParameters[0].value.firstInput);
console.log(fromToParameters[0].value.firstInput === undefined);
console.log(typeof fromToParameters[0].value.firstInput == 'undefined');
console.log(fromToParameters[0].value.firstInput !== undefined);
console.log(typeof fromToParameters[0].value.firstInput != 'undefined');
false
true
false
true
false
true
is it a question of react being different from js? why can't I do the same as in these stackoverflow threads?
UPDATE :
so you cannot point to a missing array element at all.
Check answers below.
I think I'll be using an array.lenght stored in a const that I then check my increment against it within my "for" loop to allow or disallow modifying my array entries on a case-by-case basis.
it's really annoying that you can't just ask js if a damn var of an unexisting array index exists or not.
it seems like this would be straightforward stuff : can't point to the index? then NO. no not this variable nor any other variable exists at this index. end of.
the guys at js definitely should put in a note for adding something as simple as this.
I'm tempted to post my code as I have something that allows for me to do what I want (calling a index with lots of undefineds and getting a object with ""s instead) but it's a bit monstrous.
You should do like this:
console.log(fromToParameters[7] && fromToParameters[7].value.firstInput);
console.log(!!fromToParameters[7] && fromToParameters[7].value.firstInput);
console.log( fromToParameters[7]&& typeof fromToParameters[7].value.firstInput == 'undefined');
I have just added check. So if fromToParameters[7] is undefined or null, your code will not break.
Check for the index first (ex: console.log(!yourArray[x]), then depending if that test passes or fails, access/add the indexes/properties you want.
I have a parameter in report that I recently changed it to accept NULL values. It was a required parameter before but now it is not required. Header in the report has below expression to read data from the parameter value. Ever since I modified the parameter to accept NULLvalues, it is erroring out. It shows #Error in the header. I have tried modifying the expression to display result even if the parameter is 'NULL', but nothing has worked so far. I am pretty much stuck here. Here is the expression I am using in SSRS:
=IIF(Code.MultipleValues(Parameters!ProductID.Value(0))="True"
,IIF(IsNothing(First(Fields!ProductName.Value, "ProductHeader")),""
,First(Fields!ProductName.Value, "ProductHeader")).ToString
,IIF(Len(Parameters!ProductID.Value(0)) > 0
,IIF(First(Fields!ProductName.Value, "ProductHeader") is nothing,""
,First(Fields!ProductName.Value, "ProductHeader")),"All Products Selected"))
I believe the error has to do with the VB custom code that is in the report. I am not too familar with the code so I am having trouble troubleshooting it.
VB code:
Public Function MultipleValues(ByVal str As String) As String
If str.Contains(",")
Return "True"
Else
Return "False"
End If
End Function
Any suggestion on how to handle #Error in SSRS? thanks
Replace
Code.MultipleValues(Parameters!ProductID.Value(0))="True"
with
IndexOf(Parameters!ProductID.Value(0).ToString(),",") >= 0
Instead of a function, can you just put the IsNothing check in front of the parameter ?
=IIF(Code.MultipleValues(IsNothing(Parameters!ProductID.Value(0),""))="True"
Okay, I've been bashing my head bloody on this one:
I have the following JSON coming back from the server:
{
"SendDate" : "2015-03-16T22:48:27.747",
"SendTo" : {
"ContactIds" : ["28a24538-cdfc-4453-920d-86f57d7eaf22"],
"GroupIds" : []
},
"Message" : "MEETING TIME!!!!!"
}
I have checked this with several REST clients - this IS what comes back.
I have AngularJS "getting" this with an $http.get() operation, but I get an undefined on the "ContactIds" value - so, what I see in the JS Console is:
SendDate : "2015-03-16T22:48:27.747"
SendTo:
ContactIds: Array[1]
0: undefined
length: 1
I have NO IDEA what can be causing this.
Any ideas?
UPDATE:
I have attached an interceptor and intercepted the response and the result is the same when I feed the data to the console - but when I use:
JSON.stringify(data)
I can see that the Data in the Array is THERE!
UPDATE 2:
Okay now this is driving me nuts. I have played with the interceptor and if I stringify the response and then use JSON.parse() - it works fine, but when I pass the response through, it comes out messed up again.
I traced it through angular's parsing process all the way to the "fromJson()" function. (code below:) It comes into the function as a string. (Now here's the Bizzarro part)
I altered the code like this:
function fromJson(json) {
var obj1 = JSON.parse(json);
console.log("Obj1:");
console.log(obj1);
//my altered angular code
var obj2 = isString(json) ? JSON.parse(json) : json;
console.log("Obj2:");
console.log(obj2);
// Pass to program...
return obj1;
//return obj2;
/* original angular code:
return isString(json)
? JSON.parse(json)
: json;
*/
}
If I run it and return obj1, the console logs obj1's ContactIds "0" index as "undefined" - but obj2 logs as "28a24538-cdfc-4453-920d-86f57d7eaf22".
"GREAT!", I'm thinking - so I return obj2, but now it logs undefined but obj1's "0" index is now the correct value. (WTH?)
So I reverse the code, just to see, and Return obj1 - and I'll be damned - obj2 returns "28a24538-cdfc-4453-920d-86f57d7eaf22" and obj1 is undefined. (It's like teasing a monkey.)
It HAS to be something later on in the pipeline that is doing it - OR - it may have something to do with the array being GUID strings - but I use GUID strings elsewhere with no problems.
It could also be another "angular process" that I'm unaware of that is causing this - angular is quite impressive.
Either way, I'm super-confused.
This is so stupid - I'm surprised that an array of strings is such a difficulty - and what's worse, it seems I'm the only one having this problem. (I researched this for six hours yesterday...)
Any other ideas, guys?
OH MY GOD I'M SO STUPID!!!
First I'd like to thank everyone for trying to help me.
The answer is this - there was no problem, that is, not with Angular or its parser.
The Problem is that the Console was logging transformed data, which had an error at MY controller. The data on MY end was not matching the data in the list that I had in my controller - (Database Error).
BOTTOM LINE:
If you have this error pop up, do NOT troubleshoot from the top-down - troubleshoot from the bottom-up.
Angular has many checks and balances - if you don't get "bleeding failures" throughout the program, chances are the error is in YOUR code.
Thank you everyone for your help.
I'm working in order to connect my client to my server (node.js). I use this code :
var storeEmployees = new qx.data.store.Json("Load/Infos");
qx.event.Registration.addListener(storeEmployees, "loaded", function(){
var model = this.getModel();
console.log(model.getRecords());
console.log(model.getTotal());
console.log(model.getStatus());
}, storeEmployees);
My server send this value :
{records: ["bonjour", "aurevoir"], total:2, status:"success"}
however the "console.log(model.getRecords())" write on the console :
Object[undefined, undefined]
Instead of
Object["bonjour","aurevoir"]
Values for "getTotal" and "getStatus" are good. The problem is only for the array (simple array and complexe array).
Any idea ?
Thanks in advance !
The store marshals the data to model objects. This means that you are dealing with qx.data.Array here which unfortunately can not be accessed vie brackets notation (e.g. Data[0]). But this is what the console does. For debugging and logging you can access the plain array with the .toArray() method which then will show the results.
Is it possible to have multiple statements in an inline if statement in an AngularJS expression? For example, the following fail:
ng-change="someCondition() ? doA(); doB() : doC()"
ng-change="someCondition() ? doA(), doB() : doC()"
This can be done as follows:
ng-change="someCondition() ? doA() : doC(); someCondition() ? doB() : ''"
But that's ugly, calls someCondition() twice, and screams to be done in a controller. Is there any way to get the first examples to work?
Angular expressions are limited to simple expressions. Anything more complicated may be rejected by parser and error will be thrown. Read about differences between Angular Expressions vs. JavaScript Expressions in documentation. Namely:
No Control Flow Statements: You cannot use the following in an Angular expression: conditionals, loops, or exceptions.
No Comma And Void Operators: You cannot use , or void in an Angular expression.
etc.
The best thing you can do is to create a function in controller that will combine doA and doB. Not only it will make Angular parse happy, but the code itself will become cleaner and simper to read. After all this is template, it's better to keep it simple.
Your first example is not valid javascript.
Your second example contains , which cannot be in any angular expression (not sure if valid javascript or not).
Depending on your use case you can sometimes get it to work using && and ||.
However, code in templates is still code. Try to make it readable. If you can't make it readable, extract it into a function in your controller.
ng-change="onChange()"
Please try the below scenario:::
use the () in multiple statements
ng-change="someCondition ? (doA(), doB()) : doC()"
ng-change="someCondition ? (doA(), doB()) : doC()"
I tried something like this in Angular 8 with ternary operator
<input
maxlength="{{memberProofForm.proofTypeId === 1?16:(memberProofForm.proofTypeId === 2?10:(memberProofForm.proofTypeId === 3?15:77))}}"
name="proofIdNumber"
required
type="text"
[(ngModel)]="memberProofForm.proofIdNumber">
It will work like this
if(memberProofForm.proofTypeId === 1){
return 16;
}
}else{
if(memberProofForm.proofTypeId === 2){
return 10;
}else{
if(memberProofForm.proofTypeId === 3){
return 15
}else{
return 77;
}
}
}