Can't check if array is initialized or empty with ngIf - angularjs

I have a property on my component for alert messages that may get populated:
alerts: ReportAlertMessage[];
I am trying to us ngIf to display an output if the array has any data.
I first tried:
<div class="row" *ngIf="alerts">
But it didn't work, it returns this error:
Cannot read property 'nativeElement' of undefined
So I tried:
<div class="row" *ngIf="typeof(alerts) !== 'undefined'">
and this gives me:
self.context.typeof is not a function
I want to check that it is initialized and has at least 1 value. What is the way to do this with ngIf?
edit:
full exception message. Problem seems to be ocurring because I am populating the array with an observable:
EXCEPTION: Uncaught (in promise): Error: Error in :0:0 caused by: Cannot read property 'nativeElement' of undefined
Error: Error in :0:0 caused by: Cannot read property 'nativeElement' of undefined
at ViewWrappedError.ZoneAwareError (http://localhost:4200/polyfills.bundle.js:3561:33)
at ViewWrappedError.BaseError [as constructor] (http://localhost:4200/vendor.bundle.js:30581:16)
at ViewWrappedError.WrappedError [as constructor] (http://localhost:4200/vendor.bundle.js:30646:16)
at new ViewWrappedError (http://localhost:4200/vendor.bundle.js:61302:16)
at CompiledTemplate.proxyViewClass.DebugAppView._rethrowWithContext (http://localhost:4200/vendor.bundle.js:83662:23)
at CompiledTemplate.proxyViewClass.DebugAppView.detectChanges (http://localhost:4200/vendor.bundle.js:83635:18)
at ViewRef_.detectChanges (http://localhost:4200/vendor.bundle.js:62229:20)
at RouterOutlet.activate (http://localhost:4200/vendor.bundle.js:67737:42)
at ActivateRoutes.placeComponentIntoOutlet (http://localhost:4200/vendor.bundle.js:25393:16)
at ActivateRoutes.activateRoutes (http://localhost:4200/vendor.bundle.js:25360:26)
at http://localhost:4200/vendor.bundle.js:25296:58
at Array.forEach (native)
at ActivateRoutes.activateChildRoutes (http://localhost:4200/vendor.bundle.js:25296:29)
at ActivateRoutes.activate (http://localhost:4200/vendor.bundle.js:25270:14)
at http://localhost:4200/vendor.bundle.js:24830:22
I have also tried writing a static bool. It appears that this error occurs when I set the value of a property inside the callback from an observable

Alternatively you can call a method in your component as
<div class="row" *ngIf="isEmpty(alerts)">
Component function
isEmpty(alerts){
if(alerts===[]) {
return true;
} else {
return false;
}
}

Seems the first is the correct way to do it. It the alerts access modifier public

Try this
<div class="row" *ngIf="alerts.length">

Related

How do I set the value of p-checkbox programmatically

The following code does not produce a error but nothing happens. I'm using ng v11, reactive forms, and primeng 11. What am I missing?
<p-checkbox label="Confirm"
formControlName="chkConfirm">
</p-checkbox>
typescript:
this.chkConfirm?.setValue(true);
edit:
Adding error message found in console:
ERROR TypeError: this.model.indexOf is not a function
at Checkbox.isChecked (primeng-checkbox.js:78)
at Checkbox.writeValue (primeng-checkbox.js:101)
at setUpControl (forms.js:2379)
at FormGroupDirective.addControl (forms.js:5710)
at FormControlName._setUpControl (forms.js:6285)
at FormControlName.ngOnChanges (forms.js:6230)
at FormControlName.rememberChangeHistoryAndInvokeOnChangesHook (core.js:1498)
at callHook (core.js:2517)
at callHooks (core.js:2484)
at executeInitAndCheckHooks (core.js:2435)

Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Cannot read property 'defaultView' of undefined [duplicate]

I am trying to use Selenium for Python eith the Chrome webdriver to automate the download of a file.
My program works perfectly up to the last step (clicking on the 'download' button) at which point a dialog box is triggered with the text:
"An error has occured in 'site url': Uncaught TypeError: Cannot read property 'getColomnSet' of undefined41"
What does this error mean and what are the most probable causes?
For reference, here are the last few commands of my program:
try:
elem = wait.until(EC.presence_of_element_located((By.ID,'element_1_id')))
finally:
elem1 = driver.find_element_by_id('element_1_id')
elem2 = driver.find_element_by_id('element_2_id')
action = ActionChains(driver).move_to_element(elem1).move_to_element(elem2)
action.perform()
elem2.click()
This error message...
An error has occured in 'site url': Uncaught TypeError: Cannot read property 'getColomnSet' of undefined
...implies that your program was unable to read the property getColomnSet while trying to download the desired file.
Possibly, the main problem is the js involved to download the document is invoked before the client renders the HTML DOM completely.
The relevant HTML, a bit more of your previous lines of code and the error stack trace would have given us some more idea about what's going wrong.
Solution
You can induce some measures to wait till the complete DOM Tree is rendered following the discussion:
Generic funtion to check if page has completely loaded in Selenium
A couple of facts:
In your code trials I don't see you interacting with the element (By.ID,'element_1_id') so possibly you can remove the step of presence_of_element_located() for the element (By.ID,'element_1_id').
If you still require presence_of_element_located((By.ID,'element_1_id')) catch the exception and initiate required steps.
As you invoke move_to_element() over elem1 and elem2 and moving ahead invoke perform() you need to induce WebDriverWait with expected_conditions as element_to_be_clickable(locator)
tl;dr (references)
Cannot read property 'getContext' of null, using canvas
Uncaught TypeError: Cannot read property 'getContext' of undefined
Uncaught Error remote.js
Try the following - it works on Salesforce Lightning UI screens:
WebElement element = driver.findElement(By.id("your ID"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);

Unable to get property 'remove' of undefined or null reference

According to my client-side error logs, my ReactJS app is sometimes throwing the following exception.
TypeError: Unable to get property 'remove' of undefined or null reference
at M.willDeleteListener (eval code:17:25544)
at v.deleteAllListeners (eval code:1:25843)
at m.Mixin.unmountComponent (eval code:16:15442)
at i.unmountComponent (eval code:15:5262)
at unmountChildren (eval code:17:3368)
at _.Mixin.unmountChildren (eval code:17:2153)
at m.Mixin.unmountComponent (eval code:16:15442)
at i.unmountComponent (eval code:15:5262)
at _.unmountComponent (eval code:15:16416)
at i.unmountComponent (eval code:15:5262)
I think the source of the error is in https://github.com/facebook/react/blob/35962a00084382b49d1f9e3bd36612925f360e5b/src/renderers/dom/client/eventPlugins/SimpleEventPlugin.js#L600
I've been unable to reproduce (either locally or in production).
What could be causing this problem to happen? I need some ideas of things to try that could reproduce the error.
I came acrossed above error "Unable to get property 'remove' of undefined or null reference" in IE11 for angular 6 application. I was using NgClass directive to set the CSS class dynamically for SVG elements.
<svg viewBox="0 0 145 110" width="145" height="110" id="svg1">
<path class="bg" [ngClass]="{'my-class': my-cond = 'condition'}" stroke="#ccc" />
</svg>
Solution: IE10 and IE11 requires the following npm, for NgClass to support on SVG elements.
- Run > npm install --save classlist.js
- import 'classlist.js'; // In polyfills.js
I've gotten this error a few times now, and it seems to happen when I'm accessing a property that doesn't yet exist in the render function. My application is using React/Redux and ImmmutableJS, and there are many AJAX calls that happen on mount of any given component, so I've got to be sure that I'm breaking out of the render function until everything is finished loading. For example, I get the error in question when I have this:
render () {
const displayName = this.props.myObject.get('displayName')
if (this.props.loading) return <Loading />
return <div>{displayName}</div>
}
But not when I switch around the loading check:
render () {
if (this.props.loading) return <Loading />
const displayName = this.props.myObject.get('displayName')
return <div>{displayName}</div>
}
Checking for existence of the object also works to get rid of this pesky error.
Piggybacking on #LaurelB's response, I diagnosed this to happen on React elements when I toggle the function of an event bind (say onClick) based on some variable whose value changes. I realized this might not be good practice, after all.

Syntax error in order() method using ng-click and ng-repeat

I am using the following html code to create an ng-repeat:
<td ng-repeat="parentCategory in totalParentCategoriesCount | orderBy :'-Count'">
<h3>
Kategori
<span class="sortorder" ng-show="predicate === 'CategoryParent{{parentCategory.Count}}'" ng-class="{reverse:reverse}"></span>
</h3>
</td>
I wish to add the text CategoryParent with the number from parentCategory.Count to create a dynamic link.
When inspecting the link in the browser I get the corret outcome which is CategoryParent0, CategoryParent1, CategoryParent2 and so on. But when clicking the link I get the following error:
Error: [$parse:syntax] http://errors.angularjs.org/1.2.16/$parse/syntax?p0=%7B&p1=is%20an%20unexpe…Parent%7B%7BparentCategory.Count%7D%7D&p4=%7B%7BparentCategory.Count%7D%7D
at Error (native)
at http://localhost:51976/Scripts/angular.min.js:6:450
at $a.throwError (http://localhost:51976/Scripts/angular.min.js:165:141)
at $a.parse (http://localhost:51976/Scripts/angular.min.js:164:6)
at $get (http://localhost:51976/Scripts/angular.min.js:96:122)
at http://localhost:51976/Scripts/angular.min.js:129:290
at http://localhost:51976/Scripts/angular.min.js:10:248
at Array.forEach (native)
at q (http://localhost:51976/Scripts/angular.min.js:7:280)
at Uc (http://localhost:51976/Scripts/angular.min.js:10:219)
On the angular page its says the following:
Syntax Error: Token '{' is an unexpected token at column 15 of the expression [CategoryParent{{parentCategory.Count}}] starting at [{{parentCategory.Count}}].
I have been looking at this answer AngularJS expression throwing a Syntax error in ng-click and therefore tried many different compinations in the order() method but I cant figure out how to make the link dynamic without using brackets.
Right now the parentCategory.Count is an int but I have also tried to parse it to a string but without any success.
ng-click already takes an Angular expression - so no need for the {{}}
ng-click="order('CategoryParent' + parentCategory.Count)"

Uncaught TypeError: Cannot read property 'style' of undefined

I got error:
**Uncaught TypeError: Cannot read property 'style' of undefined**
when trying to add a child component to a container which has accordion layout.
Below is the error stack call:
Element.override.setHeight ext-all-dev.js:24941
Ext.define.setAttr ext-all-dev.js:60477
Ext.define.applyPendingAttrs ext-all-dev.js:79673
Ext.define.runner ext-all-dev.js:79546
Ext.define.flushAnimations ext-all-dev.js:81227
Ext.define.runComplete ext-all-dev.js:81865
callOverrideParent ext-all-dev.js:35
Base.implement.callParent ext-all-dev.js:6171
Ext.override.runComplete ext-all-dev.js:64510
Ext.define.run ext-all-dev.js:81842
Ext.define.statics.flushLayouts ext-all-dev.js:64514
Ext.define.statics.resumeLayouts ext-all-dev.js:64521
Ext.resumeLayouts
Could you help me find possible reasons? Thanks!

Resources