How can I solve "Cannot call method "addL" of undefined" - javascript

I was trying to make by myself a "library" for my personal project which uses local storage for nearly everything and I got this error.
The Browser I am using is Google Chrome on the last version. It says no error line on the console and the error is:
TypeError: Cannot call method 'addL' of undefined.
JavaScript
function local (title) {
var storeTitle = title;
this.addL = function(lString) {
var storeText = lString;
localStorage.setItem(storeTitle, storeText);
};
this.removeL = function() {
localStorage.removeItem(storeTitle);
};
this.getL = function () {
localStorage.getItem(storeTitle);
};
};
I can't find the error and when I google Cannot call method ... of undefined it shows a lot of pages but with different content, not the one I'm looking for. I found from Google Maps API to jQuery API.
I learned this "way" from another question here StackOverflow.

You are missing the new keyword. So try this:
new local("locally").addL("stored")

Related

How can I set the getDownloadURL to a variable?

I am using angularFireStorage (in my Ionic app).
I am having some success, for instance, I can look at my console.log(res) to click the link in my console to view a belt image. I cannot save it to a variable however, which is the issue. Ultimately I would like to use the image tag and link the belt image using src. Here is my code:
const ref = this.storage.ref('belts/');
ref.listAll().subscribe(belt=>
belt.items.forEach(item=> printImage(item))
)
function printImage(imageRef){
let temp = imageRef.getDownloadURL();
temp.then(res=> {
console.log(res)
this.mySRCs.push(res)
})
}
and how I am testing it.
<img [src]="sanitizer.bypassSecurityTrustResourceUrl(mySRCs[0])"/>
and the error:
Uncaught (in promise): TypeError: Cannot read property 'mySRCs' of undefined
TypeError: Cannot read property 'mySRCs' of undefined
I feel like I did something similar a month or two ago but this time I cannot see the image in my app. Thanks for your help.
I think your issue is with using the 'this' keyword within your 'printImage' function.
You either need to do:
belt.items.forEach(item=> printImage.bind(this, item))
or assuming you have a...
const mySRCs = [];
then you just pass the field as an argument to the printImage function...
belt.items.forEach(item=> printImage(item, mySRCs))
and instead of using 'this' inside the function, you use...
function printImage(imageRef, mySRCs){
let temp = imageRef.getDownloadURL();
temp.then(res=> {
console.log(res)
mySRCs.push(res) //<--- remove this
})
}

Google Platform.js suddenly throwing _.Rp undefined exception decipte no change in code

From 19 July 23.45 GTM +1 all our applications using platform.js (live and in testing) started throwing exceptions on loading gapi features found in platform.js. No code change has been made, and the error started happening everywhere at once.
app.js:178 Uncaught TypeError: Cannot read property 'init' of undefined (referring to gapi.auth.init)
uncaught TypeError: _.Rp is not a function
Both errors originate from platform.js
I've attempted to rebuild the fronted js project with no luck, have also tried adding <script src="https://apis.google.com/js/client.js"></script> before loading platform.js, but to no avail.
It is now 1.30 AM, thanks in advance, from a very tired developer.
Following is a screenshot of the errors
loadGoogleApi: function (callback, context) {
var self = this;
if (typeof gapi == 'undefined') {
callback.apply(context || self);
return;
}
return gapi.load('auth2', function() {
self.auth2 = gapi.auth2.init({
scope: 'profile email',
client_id: Core.googleClientId
});
self.auth2.currentUser.listen(function (user) {
debug.log('Google user', user);
app.googleUser = user;
});
callback.apply(context || self);
});
Actually we found the issue.
It was a conflict we another JS script we are using.
In this custom JS, we created a function called Map() and it seems that google has updated its library last night and now is also using a function called Map.
We just rename our custom JS function, and it is working.

Script not printing error in JavaScript Chrome console

I have just spent far too long trying to find a problem with the code below.
In turned out that because of the context that addRoute was called in, keys() was not returning the keys of the results object. To fix this I had to use Object.keys() despite it working without a problem in the JavaScript console (which I later realised was because of the context).
My question is, why didn't this show in my JavaScript console? It took me quite a while to realise (I have cropped the full code, the actual function is a lot bigger).
Wrong, but no error in the console:
Map.prototype.addRoute = function (results) {
var sectionsIDs = keys(results);
}
Correct
Map.prototype.addRoute = function (results) {
var sectionsIDs = Object.keys(results);
}
Your first function uses the keys console API function.
That "Command Line API Reference" page includes the warning:
Note: This API is only available from within the console itself. You cannot access the Command Line API from scripts on the page.
Thus, it is by design that the keys function only exists for code run directly on the console.
Chrome gives you a small hint about the keys function being a console-only function if you view it in the console:
> keys
function keys(object) { [Command Line API] }

Facebook's GraphRequestManager() undefined, but should be working?

Facebook authentication works great, but when I go to make a Graph request:
const infoRequest = new GraphRequest(
'/me',
{
accessToken: tokenString,
fields: 'email,first_name,name,last_name,picture'
},
this._responseInfoCallback
);
new GraphRequestManager().addRequest(infoRequest).start();
}
I get "TypeError: Cannot read property 'start' of undefined"
Using the Chrome debugger, everything appears like it should be working, and even storing GraphRequestManager as a variable has everything it needs until I call start(). Even using sample code I've found on this site fails when I call start(). Am I missing something obvious?
Here's a little more info. I'm importing with this:
const FBSDK = require('react-native-fbsdk');
const {
GraphRequest,
GraphRequestManager
} = FBSDK;
If I use FBGraphRequest and FBGraphRequestManager instead of above, I get errors stating that GraphReqest, etc aren't functions when I use them. Otherwise, the rest of my code looks very similar to Shivam's, but I get 'Cannot read property 'start' of undefined', so obviously the GraphRequestManager call is failing or isn't defined (which makes no sense to me).
In my experience, the fields you wish to request are specified inside parameters:
AccessToken.getCurrentAccessToken().then(
(data) => {
let accessToken = data.accessToken
const infoRequest = new GraphRequest(
'/me',{
accessToken: accessToken.toString(),
parameters: {
fields: {
string: 'email,first_name,name,last_name,picture'
}
}
},
this._responseInfoCallback.bind(this),
);
new GraphRequestManager().addRequest(infoRequest).start();
})
Ps where did you place the functions? Are they inside the class?

How to instantiate a WinMD file to a Windows Store App Project

Hi have an Windows Store App that need to reference a single Winmd file.
I've added it adding as a normal DLL, that's the correct way?
As my understanding I'm trying to "instantiate" it on my default.js as below:
var helloTest = new WinJS.HelloWR.HelloRT();
With that I'm able to access its methods as:
var ret = helloTest.helloRt();
The problem happens when I try to run it, it raises the following exception:
0x800a138f - JavaScript runtime error: Unable to get property 'HelloRT' of undefined or null reference
It means that my HelloWR namespace is undefined, I've been looking around google for some similar sample but I've found anything.
Let me know if anyone need more info about the content of this winmd.
For those whom faces the same problem, I just did as below:
var helloTest = new HelloWR.HelloRT;
It's not needed to put WinJS as the question at the Top.
Now I'm able to access its methods:
var ret = helloTest.helloRt();
//ret returns 'HelloRT'
If someone explain it better I will check as answer.

Categories

Resources