I'm new to javascript so can anyone help me figure out why this code is not working?
I have a class and it calls a cordova barcode scanning function. I've got an example that works, however I want to be able to separate out the function(result) and function(error) and use onSuccess(result) and onFailure(error).
I have no idea why this is happening so if anyone can help that would be great.
EDIT: so ive updated the code based on Stradosphere said however im still getting result is not defined errors.
Full error message:
Uncaught ReferenceError: result is not defined at barcodeScanner.scanBarcode (barcodeScanner.js:10) at HTMLButtonElement.myFunction (main.js:18)
var me = this;
class barcodeScanner {
constructor() {
this._barcodeResult = 0;
}
scanBarcode() {
//THIS THROWS result is not defined error
cordova.plugins.barcodeScanner.scan(me.onSuccess(result), me.onFailure(error));
//THIS WORKS
cordova.plugins.barcodeScanner.scan(
function (result) {
me._barcodeResult = result.text;
alert("Barcode Scanned:" + me._barcodeResult);
},
function (error) {
alert("Scanning failed: " + error);
}
);
}
onSuccess(result) {
this._barcodeResult = result.text;
alert("Barcode Scanned:" + this._barcodeResult);
}
onFailure(error) {
alert("Scanning failed: " + error);
}
}
Looking at the docs, it appears that cordova.plugins.barcodeScanner.scan() expects you to pass a function into it. But you are calling it like this:
cordova.plugins.barcodeScanner.scan(me.onSuccess(result), me.onFailure(error));
This is passing the result of the function .onSuccess(result), but result is not defined, so you are getting an error. Additionally, you want this to be the class instance, but by defining me as this outside the class, me won't equal the class instance like you want it to. But you don't need it anyway.
Try passing functions in instead:
cordova.plugins.barcodeScanner.scan((result) => this.onSuccess(result),(error)=> this.onFailure(error))
Maybe a scope issue on your use of this. Try:
var me = this; //(put this at class level)
cordova.plugins.barcodeScanner.scan(me.onSuccess, me.onFailure);
Related
I cannot get this working. It might be simple but there is just no information online that works.
Here is what I am trying to do.
class Test {
componentDidMount() {
this.function1();
}
function1() {
var myListener = listener.on(something) {
console.log('function1 triggered');
key = 'djh3489739082';
name = 'gary';
email = 'gary#email.com';
this.function2(key, name, email);
}
}
function2 = ({ key, name, email }) => {
console.log('key: ' + key);
console.log('name: ' + name);
console.log('email: ' + email);
}
}
That's basically it. The error I get in the console is that it can't find variable this.function2.
It doesn't work when I try removing the this. prefix. I also have a much simpler function call in my code that works fine like this:
function3() {
// Some code here
this.function4();
}
function4() {
// Do some things.
}
And function3 calls function4 without issue. It's just because I need to pass parameters that it falls over and like NO ONE online needs to do this, apparently. The only examples I have found use const before the function. So like:
const function2 = ({ parameter1, parameter2, parameter3 }) => {
But this immediately throws an error. I can't use const or var or any of that stuff.
This should be easy, surely! Any ideas?
Here's what's wrong in your code. How many parameters does your function2 take? One, not three, but one. It takes a JSON object as a parameter, who has 3 properties: key, name and email, because you wrapped them with {}, but when you call it in function1, you passed 3 parameters to it, without the {}, which makes it not a JSON object.
So, you can either:
// Don't modify function2 but change function1 like so
function1() {
this.function2({ key, name, email })
}
or
// Don't modify function1 but change function2 like so
function2(key, name, email) {
}
I cannot access to enyo Ui component from function:onComplete in WebOS 3.0.
buttonTapped: function(inSender, inEvent) {
console.log("Button is clicked");
this.$.txt.setContent(inSender.name + " tapped."); // This worked
var request = webOS.service.request("luna://com.webos.service.tv.systemproperty", {
method: "getSystemInfo",
parameters: {"keys": ["modelName", "firmwareVersion", "UHD", "sdkVersion"]},
onComplete: function (inResponse) {
var isSucceeded = inResponse.returnValue;
if (isSucceeded){
console.log("Result: " + JSON.stringify(inResponse));
$.txt.setContent("Result: "+JSON.stringify(inResponse)); // This is not worked
}
}
});
...
Console output
Button clicked
Result{"modelName":"WEBOS1","firmwareVersion":"03.00.00","UHD":"false","sdkVersion":"03.00.00","returnValue":true}
Uncaught TypeError: Cannot read property 'txt' of undefined
I not found any documentation about this.
The reason for the error is that your callback function is not executing in the context of your component. this is not your component (not to mention you're missing the keyword this in front of $.txt...).
What you need to do is bind the context of the callback function or use it to create a closure over a variable that contains a reference to this.
This is a such a common occurrence that Enyo provides a utility method for this: this.bindSafely.
Try the following:
onComplete: this.bindSafely(function (inResponse) {
var isSucceeded = inResponse.returnValue;
if (isSucceeded){
console.log("Result: " + JSON.stringify(inResponse));
this.$.txt.setContent("Result: "+JSON.stringify(inResponse));
}
})
See: http://enyojs.com/docs/latest/#/kind/enyo/CoreObject/Object:bindSafely
I went though all the posts here related to this topic, but couldn't find a working solution. May be something very different in my code.
File 1, RequestFactory.js
function requestFactory() {
this.createRequest = function (reportId) {
var request;
request = new xyzRequestManager.XyzRequest();
return request;
}
return {
RequestFactory: requestFactory
}
}
File 2,request.js
function loadData() {
var request = requestFactory.createRequest(id);
request.loadReport(report);
}
File 3, xyzRequestManager.js
function () {
var xyzRequest = function() {
this.loadReport = function(report) { --some data--}
}
return {
XyzRequest: xyzRequest
}
}
So the call starts from file2, i create the request object by calling requestFactory. There are bunch of other functions written in file 3, which gets called from file 1 in similar fashion, request factory object, and make call to the function.
This gives error as,
Uncaught TypeError: xyzRequestManager.XyzRequest is not a constructor
I have wasted hours on this, and still no clue what or where am I wrong.
Any help would be appreciated.
You're returning an object with a property called XyzRequest, not xyzRequest, see the *** comment:
// Note: This is verbatim from the question other than this comment and
// the *** comment below.. It's not valid syntax on its own (the function
// would need a name), but I assume it's an excerpt from something larger.
function () {
var xyzRequest = function() {
this.loadReport = function(report) { --some data--}
}
return {
XyzRequest: xyzRequest // ***
}
}
So to use it, you need that capital X:
request = new xyzRequestManager.XyzRequest();
// -----------------------------^
Excuse the title but I'm not really sure what this is called (Maybe events?):
I have created a class that someone will use:
function cls_something()
{
this.notify('hello');
}
Now the person using my class creates a method called 'notify' (as instructed by me) in order to listen for notifications and then perform their own custom code using the param I pass:
var something = new cls_something();
something.notify = function(message)
{
console.log('The notification is ' + message);
}
How do I call this method from within the class to give him the notification message?
Fiddle
I'm trying to achieve something like this...
websocket = new WebSocket("ws://localhost:10000");
websocket.onopen = function(e)
{
console.log('you are connected');
}
websocket.onmessage = function(e)
{
console.log('omg wtf ffs, there was an error: ' + e.msg);
}
You can just call this.notify("Message"); You'd probably want to check to see if it's defined first, though.
EDIT 1:
Ok, so your problem here is that you're calling a function straight from the constructor, before it's defined. If it needs to be defined in the constructor, then pass the function as a parameter.
function cls_something(notifyFunction)
{
notifyFunction('hello');
}
EDIT 2:
Just so we're clear, you can have the user of your class define functions later, if you'd like. You just can't run them straight from the constructor, obviously. If you run them from the constructor, they need to be defined before hand.
Say your class was something like
function cls_something()
{
this.someFunctionThatIsRunLater = function() {
this.notify('hello');
}
}
Then your client can write
var something = new cls_something();
something.notify = function(message)
{
console.log('The notification is ' + message);
}
Then, when the client calls
something.someFunctionThatIsRunLater();
Notify will be called.
Ext.define('MyApp.util.batch', {
singleton : true,
config:{
},
testFun: function(){
multi =new MyApp.util.batch.runBatchProcess();
var run = function (delay) {
Ext.create('Ext.util.DelayedTask', function () {
multi.start();
run(100000);
}).delay(delay);
};
run(3000);
},
runBatchProcess: function(){
var batchObj=new Object();
var start = function(){
console.log('start');
if(Ext.device.Connection.isOnline()) { //error at this line
alert('isonline');
} else {
alert('offline');
}
};
batchObj.start = start;
return batchObj;
}
});
Getting error at the line with comment.
Error at console is: Uncaught TypeError: Cannot call method 'isOnline' of undefined
Not getting the reason for it.
Any help would be appreciated.
:)
Been awhile since I have used this framework, but looks like you did not include the needed file
Ext.require('Ext.device.Connection');
For whatever reason, it doesn't seem like Ext.device.Connection isn't included inside the default sencha-touch-all-debug.js and sencha-touch-all.js files that come w/ the download of the latest version (2.3.1) of Sencha Touch... so you'll have to include it manually.