Firebase Data validation not matching with data - javascript

I have a click button which is validating data from firebase firestore, but it is not giving the correct output as in the checks applied are getting failed.
createAptBtn.onclick = () => {
if (category.value === "" || email.value === "" || day.value === "" || time.value === "") {
promptContent.innerText = "All fields are required."
}
else if (category.value === "New") {
db.collection("recordsDb").onSnapshot((querySnapshot) => {
querySnapshot.forEach((doc) => {
//Condtion 1
if (email.value === doc.data().email && day.value === doc.data().day && time.value === doc.data().time) {
promptContent.innerText = "Email already exists."
}
//Condition 2
if (email.value !== doc.data().email && day.value === doc.data().day && time.value === doc.data().aptTimeSlot) {
promptContent.innerText = "Slot already filled."
}
//Condition 3
if (email.value === doc.data().email || mobile.value === doc.data().mobile) {
promptContent.innerText = "User already exits."
}
// Condition 4
if(email.value !== doc.data().email && day.value !== doc.data().day && time.value !== doc.data().time) {
promptContent.innerText = "Account created."
}
})
})
}
}
Also even when one of the condition is true it still outputs the else statement which should work when all ifs are getting failed.
However, condition 4 is an else statement but since its not working I'm using it as an if statement by adding some checks in it which though fails.
How do I fix this?

Related

Javascript filter between two string dates in Vue component

I'm working with Vue in a Laravel app. Everything below works except the last one. I can't seem to find the right search terms to fit this situation. Sorry if it's a duplicate.
Here is my current code:
return [...this.tableData].filter((salesorders) => {
if (this.selectOption == '6') {
return salesorders.order_status.match(this.status);
}
if (this.selectOption == '1') {
return salesorders.number.includes(this.searchInput);
}
if (this.selectOption == '2' && this.choice == 'is') {
var ship_date = moment(String(this.first_date)).format('MM-DD-YYYY');
return salesorders.requested_ship_date.match(ship_date);
}
if (this.selectOption == '2' && this.choice == 'is not') {
var ship_date = moment(String(this.first_date)).format('MM-DD-YYYY');
return !salesorders.requested_ship_date.match(ship_date);
}
if (this.selectOption == '2' && this.choice == 'is between') {
var ship_date1 = moment(String(this.first_date)).format('MM-DD-YYYY');
var ship_date2 = moment(String(this.end_date)).format('MM-DD-YYYY');
return salesorders.requested_ship_date >= ship_date1 && salesorders.requested_ship_date >= ship_date2;
}
});
I just figured it out:
return salesorders.requested_ship_date >= ship_date1 && salesorders.requested_ship_date <= ship_date2;

Is there a better way to change button display in javascript/Vue?

After writing so many if else, I feel very tired. I'm using Vue. The following code are written in the script section of the vue file. I get a json from file, and then read the values in json, then set what button should be display based on employee level and on application status. Is there a better way to change the button display status in Vue?
if (
(this.GLOBAL2.jsonForGlobal.employeeLevel == "1" &&
(this.GLOBAL2.jsonForGlobal.detail[this.detailId].currentStatus == "Checking" ||
this.GLOBAL2.jsonForGlobal.detail[this.detailId].currentStatus == "Pending" ||
this.GLOBAL2.jsonForGlobal.detail[this.detailId].currentStatus == "Approved")) ||
(this.GLOBAL2.jsonForGlobal.employeeLevel == "2" &&
(this.GLOBAL2.jsonForGlobal.detail[this.detailId].currentStatus == "Pending" ||
this.GLOBAL2.jsonForGlobal.detail[this.detailId].currentStatus == "Approved")) ||
(this.GLOBAL2.jsonForGlobal.employeeLevel == "3" &&
this.GLOBAL2.jsonForGlobal.detail[this.detailId].currentStatus == "Approved")
) {
this.pullDisplay = true;
} else {
this.pullDisplay = false;
};
if (
this.GLOBAL2.jsonForGlobal.employeeLevel == "1" &&
this.GLOBAL2.jsonForGlobal.detail[this.detailId].currentStatus == "Revising"
) {
this.cancelDisplay = true;
} else {
this.cancelDisplay = false;
};
if (
(this.GLOBAL2.jsonForGlobal.employeeLevel == "1" &&
this.GLOBAL2.jsonForGlobal.detail[this.detailId].currentStatus == "Revising") ||
(this.GLOBAL2.jsonForGlobal.employeeLevel == "2" &&
this.GLOBAL2.jsonForGlobal.detail[this.detailId].currentStatus == "Checking") ||
(this.GLOBAL2.jsonForGlobal.employeeLevel == "3" &&
this.GLOBAL2.jsonForGlobal.detail[this.detailId].currentStatus == "Pending")
) {
this.saveDisplay = true;
} else {
this.saveDisplay = false;
};
if (
this.GLOBAL2.jsonForGlobal.employeeLevel == "1" &&
this.GLOBAL2.jsonForGlobal.detail[this.detailId].currentStatus == "Revising"
) {
this.reviseDisplay = true;
} else {
this.reviseDisplay = false;
};
if (
(this.GLOBAL2.jsonForGlobal.employeeLevel == "2" &&
this.GLOBAL2.jsonForGlobal.detail[this.detailId].currentStatus == "Checking") ||
(this.GLOBAL2.jsonForGlobal.employeeLevel == "3" &&
this.GLOBAL2.jsonForGlobal.detail[this.detailId].currentStatus == "Pending")
) {
this.sendDisplay = true;
} else {
this.sendDisplay = false;
};
if (
(this.GLOBAL2.jsonForGlobal.employeeLevel == "3" &&
this.GLOBAL2.jsonForGlobal.detail[this.detailId].currentStatus == "Pending") ||
(this.GLOBAL2.jsonForGlobal.employeeLevel == "2" &&
this.GLOBAL2.jsonForGlobal.detail[this.detailId].currentStatus == "Checking")
) {
this.approvalDisplay = true;
} else {
this.approvalDisplay = false;
};
And also there are a few ones need three conditions:
if (
this.GLOBAL2.jsonForGlobal.employeeLevel == "3" &&
this.GLOBAL2.jsonForGlobal.detail[this.detailId].requestCategory ==
"External Request" &&
this.GLOBAL2.jsonForGlobal.detail[this.detailId].currentStatus ==
"Pending"
) {
this.returnDisplay = true;
} else {
this.returnDisplay = false;
}
Going with a configuration based approach will make your code much more easy to edit and to read.
const levels = {
'1': {
pullDisplayStatus: ['Checking', 'Pending', 'Approved'],
cancelDisplayStatus: ['Revising'],
saveDisplayStatus: ['Revising'],
reviseDisplayStatus: ['Revising'],
sendDisplayStatus: [],
approvalDisplayStatus: [],
},
'2': {
pullDisplayStatus: ['Pending', 'Approved'],
cancelDisplayStatus: [],
saveDisplayStatus: ['Checking'],
reviseDisplayStatus: [],
sendDisplayStatus: ['Checking'],
approvalDisplayStatus: ['Checking'],
},
'3': {
pullDisplayStatus: ['Approved'],
cancelDisplayStatus: [],
saveDisplayStatus: ['Pending'],
reviseDisplayStatus: [],
sendDisplayStatus: ['Pending'],
approvalDisplayStatus: ['Pending'],
},
}
const jsonForGlobal = this.GLOBAL2.jsonForGlobal;
const currentStatus = jsonForGlobal.detail[this.detailId].currentStatus;
const level = levels[jsonForGlobal.employeeLevel];
this.pullDisplay = level.pullDisplayStatus.indexOf(currentStatus) > -1;
this.cancelDisplay = level.cancelDisplayStatus.indexOf(currentStatus) > -1;
this.saveDisplay = level.cancelDisplayStatus.indexOf(currentStatus) > -1;
this.reviseDisplay = level.reviseDisplayStatus.indexOf(currentStatus) > -1;
this.sendDisplay = level.reviseDisplayStatus.indexOf(currentStatus) > -1;
If you use a property often it makes sense to introduce a local variable for it to clean things up:
const { employeeLevel, detail: { [this.detailId]: { currentStatus }}} = his.GLOBAL2.jsonForGlobal;
Secondly you don't need the if / else, you can just assign the boolean:
this.pullDisplay = (
employeeLevel == "1" && ["Checking", "Pending", "Approved"].includes(currentStatus) ||
employeeLevel == "2" && ["Pending", "Approved"].includes(currentStatus) ||
employeeLevel == "3" && currentStatus == "Approved"
)

CRM 2016 not liking Javascript using if-else

Can anyone tell me what CRM would hate about this onload function I've created?
It's telling me Form_OnLoad is not defined. Looks defined to me. It's enabled in my form onload, published, etc.
Thank you.
function Form_OnLoad() {
//Calculates total commission for AE1
// Products + Services
if (Xrm.Page.getAttribute("new_commissionproductae1").getValue() !== null) && (Xrm.Page.getAttribute("new_commissionserviceae1").getValue() !== null) && (Xrm.Page.getAttribute("new_commissionseligible").getValue() == "Yes") {
var comm1 = (Xrm.Page.getAttribute("new_commissionproductae1").getValue() + Xrm.Page.getAttribute("new_commissionserviceae1").getValue());
Xrm.Page.getAttribute("new_commissiontotalae1").setValue(comm1);
} else if {
// Products only
(Xrm.Page.getAttribute("new_commissionproductae1").getValue() !== null) && (Xrm.Page.getAttribute("new_commissionseligible").getValue() == "Yes") {
Xrm.Page.getAttribute("new_commissiontotalae1").setValue(Xrm.Page.getAttribute("new_commissionproductae1").getValue());
} else if {
// Services only
(Xrm.Page.getAttribute("new_commissionserviceae1").getValue() !== null) && (Xrm.Page.getAttribute("new_commissionseligible").getValue() == "Yes") {
Xrm.Page.getAttribute("new_commissiontotalae1").setValue(Xrm.Page.getAttribute("new_commissionserviceae1").getValue());
} else {
// Net Sales
(Xrm.Page.getAttribute("new_commissionnetae1").getValue() !== null) && (Xrm.Page.getAttribute("new_commissionseligible").getValue() == "Yes") {
Xrm.Page.getAttribute("new_commissiontotalae1").setValue(Xrm.Page.getAttribute("new_commissionserviceae1").getValue());
}
}
I believe that's because your JavaScript is not correct. Try to use following code instead of yours:
function Form_OnLoad() { //Calculates total commission for AE1
// Products + Services
if (Xrm.Page.getAttribute("new_commissionproductae1").getValue() !== null &&
Xrm.Page.getAttribute("new_commissionserviceae1").getValue() !== null &&
Xrm.Page.getAttribute("new_commissionseligible").getValue() === "Yes") {
var comm1 = Xrm.Page.getAttribute("new_commissionproductae1").getValue() + Xrm.Page.getAttribute("new_commissionserviceae1").getValue();
Xrm.Page.getAttribute("new_commissiontotalae1").setValue(comm1);
} else if (Xrm.Page.getAttribute("new_commissionproductae1").getValue() !== null &&
Xrm.Page.getAttribute("new_commissionseligible").getValue() === "Yes") {
Xrm.Page.getAttribute("new_commissiontotalae1").setValue(Xrm.Page.getAttribute("new_commissionproductae1").getValue());
} else if (Xrm.Page.getAttribute("new_commissionserviceae1").getValue() !== null &&
Xrm.Page.getAttribute("new_commissionseligible").getValue() === "Yes") {
Xrm.Page.getAttribute("new_commissiontotalae1").setValue(Xrm.Page.getAttribute("new_commissionserviceae1").getValue());
} else if (Xrm.Page.getAttribute("new_commissionnetae1").getValue() !== null &&
Xrm.Page.getAttribute("new_commissionseligible").getValue() === "Yes") {
Xrm.Page.getAttribute("new_commissiontotalae1").setValue(Xrm.Page.getAttribute("new_commissionserviceae1").getValue());
}
}

Cannot read property '0' of undefined - javascript error?

I have a drop down menu, and when I select the 'All' option, it gives me this error on the console:
TypeError: Cannot read property '0' of undefined
at n.$scope.onSearchByChanged (http://localhost:8080/js/jenkinsVersion/directives/assignment-filter.js:70:81)
So, I went to my script, function, line 70,character 81 :
$scope.onSearchByChanged = function () {
if ($scope.filter.list.searchBy == 'DEPARTMENT_CODE' && !$scope.filterScope.departments) {
$scope.loadDepartments();
} else if ($scope.filter.list.searchBy != 'DEPARTMENT_CODE') {
$scope.filter.list.departmentId = 0;
}
if ($scope.filter.list.searchBy == 'GROUP' && !$scope.filterScope.editorGroups) {
$scope.loadEditorGroups();
} else if ($scope.filter.list.searchBy != 'GROUP') {
$scope.filter.list.groupId = $scope.filterScope.editorGroups[0].id; //line 70
}
$scope.clearFilter('text');
};
if ($scope.filter.list.searchBy == 'GROUP' && !$scope.filterScope.editorGroups) {
$scope.loadEditorGroups();
}
if ($scope.filter.list.searchBy == 'DEPARTMENT_CODE' && !$scope.filterScope.departments) {
$scope.loadDepartments();
}
$scope.isStatusSelected = function (status) {
return _.indexOf($scope.filter.list.talentAssignmentStatuses, status) > -1;
};
$scope.selectTalentAssignmentStatus = function (status) {
$scope.clearFilter('text');
if ($scope.isStatusSelected(status)) {
_.remove($scope.filter.list.talentAssignmentStatuses, function (el) {
return status == el;
});
} else {
$scope.filter.list.talentAssignmentStatuses.push(status);
}
};
here is the loadEditorGroups function :
$scope.loadEditorGroups = function () {
Reference.getEditorGroups($scope, function (response) {
$scope.filterScope.editorGroups = response.list;
if ($scope.filterScope.editorGroups.length > 0) {
$scope.filter.list.groupId = $scope.filterScope.editorGroups[0].id
}
});
};
I'm still learning JS. Why is this error being thrown? When I change the value of the item I want to retrieve from that editorGroups list it just gives me the same error but with the corresponding number. Your help would be appreciated, please let me know if I can supply further information. Thank you!
Your logic for this if-else block is probably wrong:
if ($scope.filter.list.searchBy == 'GROUP' && !$scope.filterScope.editorGroups) {
$scope.loadEditorGroups();
} else if ($scope.filter.list.searchBy != 'GROUP') {
$scope.filter.list.groupId = $scope.filterScope.editorGroups[0].id; //line 70
}
The program will enter the else if block when both:
the if condition is false, that is: ($scope.filter.list.searchBy == 'GROUP' && !$scope.filterScope.editorGroups) == false
the else if condition is true, that is: ($scope.filter.list.searchBy != 'GROUP') == true
We can take these two statements and simplify them:
!($scope.filter.list.searchBy == 'GROUP' && !$scope.filterScope.editorGroups) && ($scope.filter.list.searchBy != 'GROUP')
($scope.filter.list.searchBy != 'GROUP' || $scope.filterScope.editorGroups) && ($scope.filter.list.searchBy != 'GROUP')
($scope.filter.list.searchBy != 'GROUP')
In step 2, I applied De Morgan's law to simplify !(A && B) to (!A || !B).
In step 3, I simplified the &&, since (A || B) && A is the same as just A.
So really, all we know when we enter the else if block is that searchBy != 'GROUP'. We do not know anything about editorGroups, and indeed, it may be undefined!
What you're probably looking for is:
if ($scope.filter.list.searchBy == 'GROUP' || !$scope.filterScope.editorGroups) {
$scope.loadEditorGroups();
} else if ($scope.filter.list.searchBy != 'GROUP') {
$scope.filter.list.groupId = $scope.filterScope.editorGroups[0].id;
}
Notice the || in the if condition. This ensures that the else if is executed only when ($scope.filter.list.searchBy != 'GROUP' && $scope.filterScope.editorGroups), so that editorGroups[0] will not give an error. I don't know enough if this is what you intended this code to do, so correct me when I'm wrong. :-)
70:81 means line 70, character 81.
The problem is at editorGroups[0]: If editorGroups is undefined, it cannot read editorGroups[0] because undefined has no property called 0.
Make sure that editorGroups isn't undefined and you'll be fine!

OR and AND conditions not returning as expected - What is best way to implement it?

I have an If statement that using || with an && operator e.g if((a || b) && c) however it is only works with the first condition i.e a but not with second i.e b even though running the debugger I can see that the condition is met and it goes to the correct line of code. Is there a better way to get this to work on both conditions?
code I have now:
function _getCatFormGUID(catName) {
debugger;
var dept = Browser.getValue(getElement("126D81CA203C21CF014C8A3550227892FE4B4A6A"));
if((catName == '1' && dept == "Entwicklung") || (catName == '7' && dept == "Entwicklung")){
return "A270AE7F957A74EF0842403EEA0032017567F3E8";
}
if((catName == '1' && dept != "Entwicklung") || (catName == '7' && dept != "Entwicklung")) {
return "8EDD0768A7CDF8FD8AE90DB473F41EF0B33FA14F";
}
return "";}
I have tried the following also:
if((catName == '1' || catName == '7') && dept == "Entwicklung"){
return "A270AE7F957A74EF0842403EEA0032017567F3E8";
}
and
if(catName == '1' && dept == "Entwicklung"){
return "A270AE7F957A74EF0842403EEA0032017567F3E8";
}
if(catName == '7' && dept == "Entwicklung"){
return "A270AE7F957A74EF0842403EEA0032017567F3E8";
}
It only returns for catName =='1'.
If I understood your problem correctly, I will write your first bit of code as bellow
function _getCatFormGUID(catName) {
var dept = Browser.getValue(getElement("126D81CA203C21CF014C8A3550227892FE4B4A6A"));
if (catName == '1' || catName == '7') {
if(dept == 'Entwicklung'){
return "A270AE7F957A74EF0842403EEA0032017567F3E8";
}
else{
return "8EDD0768A7CDF8FD8AE90DB473F41EF0B33FA14F";
}
}
else{
return "";
}
}
The problem is that you have to understand how those 2 logical operators do the comparison: because your first condition catName == '1' is true, it will never go to second conditions from the first parenthesis nor in the second paranthesis.Given your example, you might rewrite your logical condition from:
if((catName == '1' && dept == "Entwicklung") || (catName == '7' && dept == "Entwicklung")){
return "A270AE7F957A74EF0842403EEA0032017567F3E8";
}
to
if(dept == "Entwicklung" && catName == '1' || catName == '7'){ return something; }
}

Categories