Uncaught TypeError: Cannot call method 'put' of undefined - angularjs

I am facing this issue when i am trying to store data in my angular controller page using the $cookieStore. Are there any additional configurations that I have left out ?
function fbLogin($cookieStore){
$cookieStore.put("userid",'12345');
validateUser(userid);
alert(userid);
}

You're not actually defining userId anywhere to JavaScript. You're setting a cookie whose value is userid=12345, but then calling validateUser with an unknown variable, userId which has the undefined value.

Related

Why angularjs throw 'Illegal invocation' when using promise construction?

I invoke some promise function:
return $http.post("anyCtrl").then(location.reload);
After that I have thrown exception in browser console 'Illegal invocation' in angular.
If I invoke:
return $http.post("anyCtrl").then(function(){location.reload()});
Everything is good.
I expected that all of my code snippets should be working.
Passing location.reload as an argument works more or less the same as reassigning it. If you reassign an object's method and it's not bound, that object's this will become the object that it's assigned to. For example:
const notlocation = {};
notlocation.reload = location.reload();
notlocation.reload(); // illegal invocation
You need to invoke reload from the location object. There are a couple of ways you can do this. One is to have the parentheses with the method call explicitly as you've done:
$http.post("anyCtrl").then(() => location.reload());
Another is to use the .bind and bind it to the object you want to invoke the method:
$http.post("anyCtrl").then(location.reload.bind(location));

Backbone.js - get childElementCount value in devTools to print

I trying to log childElementCount property in chrome dev tools but I get undefined.
this is my code:
console.log('childElementCount---', this.ui.tbody.get('childElementCount')
returns childElementCount--- undefined
why is it undefined?
jQuery get() is for retrieving the underlying DOM element from the jQuery object. It accepts an integer index instead of a string.
Try this.ui.tbody.get(0).childElementCount or this.ui.tbody.get(0).getAttribute('childElementCount')
or withjQuery:
this.ui.tbody.prop('childElementCount')
or this.ui.tbody.children().length

sessionStorage returning undefined value error

I'm using angular-local-storage
But I am getting undefined value error then after refresh i get the value.
$scope.welcomeMsg = "Welcome!" + " " + sessionStorage.name;
Please help why I am getting this error.
The documentation of angular-local-storage provides
localStorageService.get(key)
as the method to retrieve values from whatever storageprovider you've configured angular-local-storage to use.
If you want to use the native sessionStorage, I believe you must use
sessionStorage.getItem(key)
to retrieve the value according to the specification.

Angular2 Object Instantiation for Asynchronous Data

I was working on an angular2 project that gets an object asynchronously from the api using the new Observable framework.
this._accountsService.getAccountDetails(this.id)
.subscribe(
AccountDetails => this.accountDetails = AccountDetails,
err => this.errorMessage = <any>err,
() => console.log(this.accountDetails)
);
When we tried to access the property of that object using interpolation {{AccountDetails.UserName}} ,we got a property undefined error, because the object had not yet been received from the api. We fixed this by instantiating the object in the class
accountDetails: AccountDetails = new AccountDetails();
This works, however I noticed in Angular 1, a fake object would be created, if the object was undefined, so that there would be never be an an undefined error -- it would just show an empty value in the interpolated code. Is this a change in Angular2? Also, I noticed that when we used *ngFor in our code to iterate through the properties of an object, it would display an empty value (rather than trigger an object undefined error) if the object had not yet been received from the api. Is this something that occurs internally in the *ngFor directive?
With first piece of code you need to do following things,
1) Because it should be {{accountDetails.UserName}} (not {{AccountDetails.UserName}} ). NOTE : Please double check with U or u that I can't tell without knowing object's properties.
2) You could also use ?. like {{accountDetails?.UserName}}
I hope this will help.
Angular 2 is based off Typescript so there is stricter type checking hence the undefined error.
One thing you can do to avoid any errors is use an
*ngIf="AccountDetails.UserName"
condition on the parent tag (parent of the *ngFor loop). This way it only gets rendered once the information has been loaded from the API. You can have some loading screen with the opposite condition
*ngIf = "!AccountDetails.UserName"
so it displays till the data has been loaded and then gets swapped out once the data has been loaded.
You can remove the
accountDetails: AccountDetails = new AccountDetails();

marionette example not working

I have all the HTML , JS code here on codepen : http://cdpn.io/nbuGB
I started learning marionette JS. Somehow this simple example is not working.
It throws error :
Uncaught TypeError: undefined is not a function
how to resolve this ?
Your problem is in these lines:
ContactManager.reqres.setHandler("contact:entities",function(){
return API.getContactEntities;
});
You need to write API.getContactEntities() otherwise you're returning a function reference instead of your contacts collection. This causes the undefined error.
See: http://codepen.io/anon/pen/jALtq

Resources