issue with $cordovaContacts - javascript

i'm new in stackoverflow forum and i decide to create this topic to solve a problem wich i'm stuck for 2 weeks.
I have the following :
Visual Studio Android Emulator
Ionic 1 project created with the tabs pattern.
I'm trying to use the ngCordova contacts module ( $cordovaContacts ) to retrieve the phone numbers of the contacts. The problem is that there is an error with the "navigator" object. I found that there is not possible to acces the fields of these object ( functions , app , contacts , splashscreen ) so when i call to $cordovaContacts.find(...) it shows the error : "Can not find property find of undefined" . I invested some time debugging and when i use :
console.log(navigator);
The console shows:
[object Object]
services.js (21,15)
CordovaNavigator
_ {_
_ [functions]: ,_
_ proto: { },_
_ app: { },_
_ contacts: { },_
_ splashscreen: { }_
_ }_
But when i print navigator.contacts object it shows undefined
I also tried to use navigator.CordovaNavigator but is also undefined.
This code is called when loading controllers.
I have no idea why it shows that navigator has an atribute contacts and then when i call it it shows UNDEFINED . Maybe i need to stablish some acces rights ??.
I'm really lost so anything you tell me will help. Thanks a lot.

If you are trying to fetch the contacts on controller load (means, that you don't trigger it manually inside another method or via a timeout / interval), you'll need to wrap it with the ionicPlatform.ready() method, else the plugin isn't ready at that point.
ionic.Platform.ready(function(){
$cordovaContacts.find().then(function(allContacts) {
$scope.contacts = allContacts;
});
});

Related

Javascript square bracket notation couldn't be evaluated properly while using an android webview. Any advice to fix it?

I'm using android webview and want to make dynamic function calls over the Javascript Interface. My interface's name is AndroidBridge. And here is my code:
AndroidBridge[key](values[key]);
It works properly on the browser. But when it comes to my webview, is throwing the error below:
Error connecting controller TypeError: AndroidBridge[key] is not a function [object Object]"
Any advice to jump over that issue?
I can give you the two possible reasons for that issue now:
Missing #JavascriptInterface annotation.
The method is missing or params not matching.
I don't know why browser's js interpreter looks to "a square named version of that object", but method names that match should work without that errors.
I can leave here a fully working example:
class JsBridge {
#JavascriptInterface
fun showToast(message: String) {
Log.d("DEBUG", "JsBridge showToast message:$message")
val currentActivity = current ?: return
Toast.makeText(currentActivity, message, Toast.LENGTH_SHORT).show()
}
companion object {
#JvmStatic
val instance: JsBridge = JsBridge()
}
}
// Somewhere in your code
webView.addJavascriptInterface(JsBridge.instance, "AndroidBridge")
Now you can call that method in the js side:
AndroidBridge.showToast('A message');
// or
AndroidBridge['showToast']('A message');
They both should work.

Nest JS Cannot read property of undefined

My application was working fine, and it stopped after trying to get the documentation with swagger, i think it may be a dependency issue, but can't find it anywhere.
I keep getting the error
10:10:22 PM - Starting compilation in watch mode...
Error Cannot read property 'getSymbol' of undefined
I don't know where getSymbol is used, and the error doesn't seem to help much. Hope someone can help me fix this issue. The complete application code can be found at:
https://github.com/JSLearningCode/enderecosAlunosAPI
Any help is welcome.
EDIT:
Running in dev mode I got this output:
/home/william/Documentos/lemobs/enderecosAlunosAPI/node_modules/typescript/lib/typescript.js:95877
throw e;
^
TypeError: Cannot read property 'getSymbol' of undefined
at Object.isArray (/home/william/Documentos/lemobs/enderecosAlunosAPI/node_modules/#nestjs/swagger/dist/plugin/utils/ast-utils.js:6:25)
at getTypeReferenceAsString (/home/william/Documentos/lemobs/enderecosAlunosAPI/node_modules/#nestjs/swagger/dist/plugin/utils/plugin-utils.js:12:21)
at Object.getTypeReferenceAsString (/home/william/Documentos/lemobs/enderecosAlunosAPI/node_modules/#nestjs/swagger/dist/plugin/utils/plugin-utils.js:31:29)
at ControllerClassVisitor.createTypePropertyAssignment (/home/william/Documentos/lemobs/enderecosAlunosAPI/node_modules/#nestjs/swagger/dist/plugin/visitors/controller-class.visitor.js:51:44)
at ControllerClassVisitor.createDecoratorObjectLiteralExpr (/home/william/Documentos/lemobs/enderecosAlunosAPI/node_modules/#nestjs/swagger/dist/plugin/visitors/controller-class.visitor.js:38:18)
at ControllerClassVisitor.addDecoratorToNode (/home/william/Documentos/lemobs/enderecosAlunosAPI/node_modules/#nestjs/swagger/dist/plugin/visitors/controller-class.visitor.js:29:22)
at visitNode (/home/william/Documentos/lemobs/enderecosAlunosAPI/node_modules/#nestjs/swagger/dist/plugin/visitors/controller-class.visitor.js:16:29)
at visitNodes (/home/william/Documentos/lemobs/enderecosAlunosAPI/node_modules/typescript/lib/typescript.js:70998:48)
at Object.visitEachChild (/home/william/Documentos/lemobs/enderecosAlunosAPI/node_modules/typescript/lib/typescript.js:71266:355)
at visitNode (/home/william/Documentos/lemobs/enderecosAlunosAPI/node_modules/#nestjs/swagger/dist/plugin/visitors/controller-class.visitor.js:18:23)
error Command failed with exit code 1.
There was an issue related to the routing in the application. I had a parser inside the controller that was used for directing correct routes between a route "aluno" with first Param.
Once I had taken the route with no params and put it first at the controller, there was no need anymore for the parser, and the issue was gone. Hope this answer helps more people if they get the same problem.
Please check your method result type of controller
Change this:
#Contoller()
export class MyController {
// ...
async myMethod() {
return {}
}
}
to:
#Contoller()
export class MyController {
// ...
async myMethod():Promise<any> {
return {}
}
}

ReactJS - External JS Function "is not a function"

In short, I'm trying to call an external JS function which my 3rd party required me to include, in order to use their API, but doesn't work as it supposed to.
From what I've read, I am supposed to use, for example, window.extFn() after including the external JS in my index.html which contains the extFn() like so
<script src="https://example.com/external.js"></script> <-- actually not like this, see update 2 as I imported the library locally
...and supposed to use it like how it was answered here: Call external Javascript function from react components regardless of whether the said function is inside a file or simply defined on index.html <script> section. This had worked on the JS file I created for testing this.
//File: test.js
function test() {
return "Hello";
}
...imported the JS file with script tag like usual, and using console.log(window.test()) in my React Component's render() returned Hello.
I tried moving the import to HTML head from body and vice-versa but the error I'm still getting is:
TypeError: window.extFn is not a function
QuickCheckout.render
....
22 | }
23 |
24 | render() {
> 25 | window.extFn({
26 |
View compiled
▶ 20 stack frames were collapsed.
And when I look into my browser console, for some reason I have (which seems to be the key problem)
Uncaught SyntaxError: Unexpected token < external.js:1
Tried console.log(window.extFn) and it returns undefined
So I think it might be possible that the said JS is the problem itself, but I'm at my wit's end with this. Meanwhile I had emailed my 3rd party support team, does anyone have any advice on this? Thank you very much.
UPDATE: Now my test.js file above, which had worked in my experiment, produces the Unexpected token < error as well in my console...
UPDATE 2: I apologize for your problems. But I actually imported the JS from local source due to having to port their library as they had jQuery 2 instead of 3.
<script src="assets/js/external.js"></script>
And to my dumbness, i forgot the trailing /. Thank you for your help.
It seems that the path of external.js is wrong, which returns a html file instead of js file
you can check what the request of external.js returns at the "network" tab in chrome dev-tool
At the begining of file, before the class definition, please add
let extFn = window.extFn
then inside of component,you can use it.
extFn()//call inside component

nightwatch.js: keyboard action

I am using nightwatch.js to write automation scripts. I want to use keyboard keys but it seems not working.
I have tried:
hitEnter: function () {
this.setValue('#submitButton', this.Keys.ENTER);
}
call this function in test_file.js as
loginPage
.hitEnter();
It gives error TypeError: Cannot read property 'ENTER' of undefined
What am I doing wrong?
this.Keys.ENTER
In this case, this = loginPage , not browser, you should execute with browser object through api :
this.setValue('#submitButton', this.api.Keys.ENTER);
edit:
api will return an object which contains " custom command/assertion" + "core command/assertion" + "global variables". Keys is core command ,based on selenium.

Javascript variable function method call

I apologise if this has been asked elsewhere, I looked but without knowing the name for what I am doing is, I couldn't find anything.
Anyways, the code is as follows:
function alertTypeOptions(AlertType, AlertOptions) {
navigator.notification.AlertType(AlertOptions);
}
This code is for a phonegap / cordova application.
The basic idea is that you pass the function a two variables and these are used to execute the appropriate method. Examples of this could be alertTypeOptions('beep', '3') or alertTypeOptions('vibrate', '2000'). This (should) play the default alert tone 3x or vibrate the phone for 2 seconds.
I am currently getting the following error:
02-21 15:36:07.185: E/Web Console(7206): Uncaught TypeError:
Object #<Object> has no method 'AlertType'
at file:///android_asset/www/res/scripts.js:181
Obviously the function is currently just using the alertType variable as written rather than as a variable.
Is there a way to get this to work elegantly? Currently my only thoughts are to use a switch statement with AlertType as the check.
Jack
It looks like you want to access your function using the bracket notation :
navigator.notification[AlertType](AlertOptions);
or rather, if I trust this documentation and if AlertOptions is an array :
navigator.notification[AlertType].apply(navigator, AlertOptions);

Categories

Resources