Uncaught ReferenceError: require is not defined - javascript

I am trying to implement cordova-plugin-email-composer.I installed the plugin using cli
cordova plugin add https://github.com/katzer/cordova-plugin-email-composer.git
I got an error Uncaught ReferenceError: require is not defined at email_composer.js:22.
In the link u can find the plugin. I added the code attached below in my index.js file. Can anyone help to solve this? Thankyou.
index.js:
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, function () {
cordova.plugins.email.isAvailable(
function (isAvailable) {
alert("is email mobile available? " + (isAvailable ? "Yes" : "No"));
if(isAvailable){
window.plugin.email.open({
to: 'anu.barbie143#gmail.com',
subject: 'Greetings',
body: 'How are you? Nice greetings from Leipzig'
}, callback, scope);
}
}
);
}, false);
function callback(){
console.log("callback function");
}
function scope(){
console.log("scope function");
}
},
email_composer.js:
var exec = require('cordova/exec'),
isAndroid = navigator.userAgent.toLowerCase().indexOf('android') > -1,
mailto = 'mailto:';
In the above code i got an error require is not defined.Can anyone help me to solve this?
Thankyou.

I made it work by doing following
cordova plugin rm cordova-plugin-email-composer
then add the plugin with version 0.8.2,by following command since there is an open error in plugin version 0.8.3 for loolipop
cordova plugin add https://github.com/katzer/cordova-plugin-email-composer.git#0.8.2
index.js
var app = {
// Application Constructor
initialize: function() {
document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
},
// deviceready Event Handler
//
// Bind any cordova events here. Common events are:
// 'pause', 'resume', etc.
onDeviceReady: function() {
this.receivedEvent('deviceready');
cordova.plugins.email.open({
to: 'test#gmail.com',
cc: 'test#gmail.com',
bcc: [],
subject: 'Greetings',
body: 'How are you? Nice greetings from Naresh'
});
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
}
};
app.initialize();
Hope it helps you..

I modified your code and its working now.
So please check it once.
cordova.plugins.email.isAvailable(function (isAvailable) {
// alert('Service is not available') unless isAvailable;
alert("is email mobile available? " + (isAvailable ? "Yes" : "No"));
if(isAvailable){
window.plugin.email.open({
to: 'test#test.com',
subject: 'Greetings',
body: 'How are you? Nice greetings from Leipzig',
}, function(){
console.log('email view dismissed');
},
this);
}
});
if alert is "no" it means you don't have any email application or configuration.

Related

Cordova aerogear-push android push is not defined

I'm trying to build a simple application on cordova using aerogear push notification plugin
What i'm doing is following closely this guide: https://aerogear.org/docs/guides/aerogear-cordova/AerogearCordovaPush/#_sample_example
However, after put the sample code in my js, this line:
push.register(onNotification, successHandler, errorHandler, pushConfig);
will cause a reference error since push is not defined
I followed all the step before and the aerogear-cordova-push plugin is in the folder of the plugins, maybe i require some additional steps to refer to the plugin?
Also, the plugin provide an index.html as example inside its folder, but even using that i'm not able to resolve push
I tried to move the js files of the plugin in the www folder and linked them on index before the execution of index.js, since this it isn't very correct cause other reference errors
The index.html on the www folder is the same that a standard cordova project provide after its creation
This is my index.js, i'm able to show the error on android throught the try catch:
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
try {
app.receivedEvent('deviceready');
var pushConfig = {
pushServerURL: "...",
android: {
senderID: "...",
variantID: "...",
variantSecret: "..."
}
};
push.register(app.onNotification, successHandler, errorHandler, pushConfig);
}
catch (e){
alert(e);
}
function successHandler() {
console.log('success')
}
function errorHandler(message) {
console.log('error ' + message);
}
},
onNotification: function(event) {
alert(event.alert);
},
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
}}; app.initialize();
I solved this because the plugin wasn't correct installed in my application, and the installation would fail because i was missing the google-service.json file that is required in order to build for android

Handling Direct Update with navigation to native page in IBM Mobile first

I am developing a project, in which i need to call a native page in wlCommonInit()
function wlCommonInit(){
WL.NativePage.show(nativePageClassName, backFromNativePage, params);
}
I want my project to receive the direct update with persession mode. So to connect with the Mobile First Server, I have called WL.Client.connect()
function wlCommonInit(){
busyind = new WL.BusyIndicator;
busyind.show();
WL.Client.connect({onSuccess: connectSuccess, onFailure: connectFail});
WL.NativePage.show(nativePageClassName, backFromNativePage, params);
}
More over I want to handle the direct update so I have added the required code.
wl_directUpdateChallengeHandler.handleDirectUpdate = function(directUpdateData,
directUpdateContext) {
// custom WL.SimpleDialog for Direct Update
var customDialogTitle = 'Custom Title Text';
var customDialogMessage = 'Custom Message Text';
var customButtonText1 = 'Update Application';
var customButtonText2 = 'Not Now';
WL.SimpleDialog.show(customDialogTitle, customDialogMessage, [{
text: customButtonText1,
handler: function() {
directUpdateContext.start(directUpdateCustomListener);
}
}, {
text: customButtonText2,
handler: function() {
wl_directUpdateChallengeHandler.submitFailure();
}
}]);
};
var directUpdateCustomListener = {
onStart: function(totalSize) {},
onProgress: function(status, totalSize, completeSize) {},
onFinish: function(status) {
WL.SimpleDialog.show('New Update Available', 'Press reload button to update to new version', [{
text: WL.ClientMessages.reload,
handler: WL.Client.reloadApp
}]);
}
};
Here the problem is, the application is navigating to the native page
before it can go to the direct update handler function when the direct
update is available.
Is there any way to resolve it?
I think what you should do instead if use the API [WL.Client.checkForDirectUpdate.
This way you will have the ability to first check for direct update - handle it if there is an update and then execute the function for opening the native page.
The code that is running is async, so you can't control it if you're not following the above suggestion.

Angular and ionicPush not working on android

Trying to migrate to ionicPush but getting errors anyway I do it:
Option 1 - Angular method
When I put $ionicPush.init as per guide in the app.js or anywhere for that matter, getting:
Uncaught TypeError: $ionicPush.init is not a function
When I check $ionicPush it has 2 methods, register and unregister. So clearly it gets imported, but for whatever reason doesnt have .init
Top of app.js looks like this:
.run(function(AppRootService, $ionicPlatform, $ionicPush, $cordovaSplashscreen,$window, $timeout) {
$ionicPlatform.ready(function() {
$ionicPush.init({
"debug": true,
"onNotification": function(notification) {
var payload = notification.payload;
console.log(notification, payload);
},
"onRegister": function(data) {
console.log(data.token);
}
});
$ionicPush.register();
Option 2 - Regular JS way
Put this code in app.js after $ionicPlatform.ready()
var push = new Ionic.Push({
"debug": true,
"onNotification": function(notification) {
var payload = notification.payload;
console.log(notification, payload);
},
"onRegister": function(data) {
console.log(data.token);
}
});
push.register(function(token) {
console.log("Device token:",token.token);
});
Ionic.io();
Still nothing, this time error is Uncaught ReferenceError: Ionic is not defined
Ran both of these:
ionic add ionic-platform-web-client
ionic plugin add phonegap-plugin-push
Moved around Ionic.io(), not luck
I think you have to init io and activate debug/dev mode.
Here is the all steps...
ionic add ionic-platform-web-client
ionic plugin add phonegap-plugin-push
ionic io init
ionic config set dev_push true
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
var push = new Ionic.Push({
"debug": true
});
push.register(function(token) {
console.log("Device token:",token.token);
});
});
})
Checkout this page for setting up push for platforms (iOS, Android)
http://docs.ionic.io/docs/push-from-scratch
This is the code i use with the phonegap-plugin-push
inside the run phase
document.addEventListener("deviceready", function () {
var androidConfig = {
'senderID': '****google-project-id****',
'sound': true,
'vibrate': true
};
var Push = PushNotification.init({
"android": androidConfig
});
Push.on('registration', function(data) {
// device token:
var deviceToken = data.registrationId;
console.log('pushToken', deviceToken);
});
Push.on('error', function(e) {
console.error('Notifications error: ', e.message, e);
});
Push.on('notification', function(response){
console.log('norification', response)
console.log('any additinal data the push recieved', response.additionalData)
});
}, false);
try what i did and it will work for you.

Phonegap + JQuery does not connect to internet

I have been unable to successfully figure out why $.ajax refuses to connect to the Internet in Phonegap. The same code runs well in a standard HTML file. Access Origin is set to *.
Originally I thought Phonegap was refusing a connection to the Internet. However, it loads JQuery and JQuery Mobile remotely (by link tag). JQuery continues to return an error. Tested in JBoss Developer Studio (PG 4.1.2) and PG by commandline 4.3.
The error returned by stringifying the error object
error: {"readyStaate":4,"responseText":"{\n \"code\":
\"ENOTFOUND\",\n \"errno\":\"ENOTFOUND\",\n \"syscall\":\:getaddrinfo\"\n}","responseJSON":{"code":"ENOTFOUND","errno":"ENOTFOUND","syscall":"getaddrinfo"},"status":500,"statusText":"error"}
The Javscript used:
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
bindEvents: function() {
document.addEventListener('load', this.onDeviceLoad, false);
document.addEventListener('deviceready', this.onDeviceReady, false);
},
onDeviceLoad: function() {
window.isAndroid = navigator.userAgent.match(/(android)/gi) != null;
},
onlineEvent: function() {
},
offlineEvent: function() {
},
onDeviceReady: function() {
app.receivedEvent('deviceready');
// online/offline must be set in onDeviceReady
document.addEventListener('online', this.onlineEvent , false);
document.addEventListener('offline', this.offlineEvent , false);
$.ajax({
method: "POST",
url: "http://www.someserver/server.php",
data: { name: "John", location: "Boston" }
})
.done(function( msg ) {
alert( "Data Saved: " + msg.posts );
console.log('NETWORK' + msg.posts);
})
.fail(function(error) {
alert("error: " + JSON.stringify(error));
});
},
receivedEvent: function(id) {
}
};
app.initialize();
Edit: I am aware of what the 500 error is. I have tested the server. It works fine with HTML5 and the same JQuery AJAX call. My hunch is that there is something with Phonegap; between the JQuery Client call and the server that is causing the error.
Edit2: The server name is private. The string is set to a real server on my actual code.
jQuery doesn't "connect to the internet". It's just a JavaScript library. The 500-status response you're getting is telling you that the server you're trying to POST to is erroring:
http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#5xx_Server_Error
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

mozilla addon. required(sdk/tabs).attach() not working

I began to develop addon for firefox and I had a problem.
var tabs = require('sdk/tabs');
tabs.on('ready', function (tab) {
tab.attach({
contentScript: "alert('azaza');",
onMessage: function(message) {
console.log("message");
}
});
})
When I try to execute this code in Firefox nightly 36 it says "TypeError: window is null", but in Nightly 32 it works fine! In last fierfox (not nightly) this code not working too.
I tried to execute this code in nightly's browser debugger console, but the same result (window is null).
I can see, that in sdk/tabs/utils.js browser.contentWindow is null. I think this is my window object, but why it is null?
I was able to reproduce this issue with the following code:
var { ActionButton } = require("sdk/ui/button/action");
var self = require("sdk/self");
var tabs = require('sdk/tabs');
var button = ActionButton({
icon: self.data.url("icon-16.png"),
id: "my-button",
label: "my button",
onClick: function() {
tabs.open({
url: self.data.url("text-entry.html")
});
tabs.activeTab.attach({
contentScript: "alert('azaza');"
});
}
});
To fix this issue I had to use onOpen instead of using activeTab:
var button = ActionButton({
icon: self.data.url("icon-16.png"),
id: "my-button",
label: "my button",
onClick: function() {
tabs.open({
url: self.data.url("text-entry.html"),
onOpen: function() {
tabs.activeTab.attach({
contentScript: "alert('azaza');"
});
}
});
}
});
Perhaps are you using the attach method when you cannot use it?

Categories

Resources