I installed the plugin OpenSettings via node.js with this command in my project:
cordova plugin add https://github.com/erikhuisman/cordova-plugin-opensettings.git
But when I use method OpenSettings.setting() logcat return me an error:
OpenSettings.settings error at
file:///android_asset/www/plugins/nl.tapme.cordova.opensettings/www/OpenSettings.js:23
This is OpenSettings.js:
cordova.define("nl.tapme.cordova.opensettings.OpenSettings", function(require, exports, module) { module.exports = OpenSettings = {};
OpenSettings.settings = function(app, callback) {
cordova.exec(
// Success callback
callback,
// Failure callback
function(err) { console.log('OpenSettins.settings error'); },
// Native Class Name
"OpenSettings",
// Name of method in native class.
"settings",
// array of args to pass to method.
[]
);
};
OpenSettings.bluetooth = function (app, callback) {
cordova.exec(
// Success callback
callback,
// Failure callback
function(err) { console.log('OpenSettings.bluetooth error'); },
// Native Class Name
"OpenSettings",
// Name of method in native class.
"bluetooth",
// array of args to pass to method.
[]
);
};
OpenSettings.bluetoothStatus = function (app, callback) {
cordova.exec(
// Success callback
callback,
// Failure callback
function(err) { console.log('OpenSettins.bluetoothStatus error'); },
// Native Class Name
"OpenSettings",
// Name of method in native class.
"bluetoothStatus",
// array of args to pass to method.
[]
);
};
OpenSettings.bluetoothChange = function (callback) {
cordova.exec(
// Success callback
callback,
// Failure callback
function(err) { console.log('OpenSettins.bluetoothChange error'); },
// Native Class Name
"OpenSettings",
// Name of method in native class.
"bluetoothChange",
// array of args to pass to method.
[]
);
};
return OpenSettings;
});
Anyone can help me?
I would suggest you to test this plugin -> https://github.com/selahssea/Cordova-open-native-settings the first one you posted already did not work for me too.
Install it like this:
cordova plugin add https://github.com/selahssea/Cordova-open-native-settings.git
and use it like this:
cordova.plugins.settings.open(settingsSuccess,settingsFail);
Full snippet:
function settingsSuccess() {
console.log('settings opened');
}
function settingsFail() {
console.log('open settings failed');
}
function openSettingsNow() {
cordova.plugins.settings.open(settingsSuccess,settingsFail);
}
The plugin will open this overview:
Related
I'm not knowledgeable in JS and JS in Node RTE. But I tried writing simple functions as arrow functions in objects for later usage. One of these arrow functions (data.volatile.modularVariables.read) calls the readFile (asynchronous) function from FileSystem node native module, and passes the following into the parameters from the same object:
file path (data.persistent.paths.srcRoot)
encoding scheme (data.volatile.modularVariables.encoding.utf8)
call-back function (data.volatile.modularVariables.readCB) <-issue lays here
relevant Code (the object):
var data =
{
persistent:
{
states:
{
//exempt for question clarity
},
paths:
{
srcRoot: './vortex/root.txt'
}
},
volatile:
{
//much of the data exempt for question clarity
modularVariables:
{
encoding: {utf8:'utf8',hex:'hex',base64:'base64',BIN:(data,encoding)=>{Buffer.from(data,encoding)}},
readCB: (err,data)=>{if(err){console.log(`%c${data.volatile.debug.debugStrings.errCodes.read}: Error reading from file`,'color: red'); console.log(data);}},
writeCB: (err)=>{if(err){console.log(`%c${data.volatile.debug.debugStrings.errCodes.write}: Error writing to file`, 'color:red')}},
read: (file,encoding,cb)=>{fs.readFile(file,encoding,cb)}, //cb = readCB READ file srcRoot, pass into root(rootHash,encoding)
write: (file,data,cb)=>{fs.writeFile(file,data,cb)}, //cb = writeCB
checkInit: (symbol)=>{if(typeof symbol !== undefined){return symbol}}
},
debug:
{
functions:
{
append:
{
testProg:{program:{main:'system.node.upgrade'}}
}
},
debugStrings:
{
errCodes:
{
read: 'AFTERNET ERR 000',
write: 'AFTERNET ERR 001',
}
}
}
}
};
Aggregator Code:
testing()
{
data.volatile.modularVariables.read(data.persistent.paths.srcRoot,data.volatile.modularVariables.encoding.utf8,data.volatile.modularVariables.readCB(data));
};
Terminal Error:
readCB: (err,data)=>{if(err){console.log(`%c${data.volatile.debug.debugStrings.errCodes.read}: Error reading from file`,'color: red'); console.log(data);}},
^
TypeError: Cannot read property 'volatile' of undefined
Notes:
In aggregator code, I tried not passing "data" into callback
I tried passing "err" but says it's undefined
I tried not passing anything into CB
Conclusion:
Can someone point out what I'm doing wrong and why?
I couldn't tell from the comments if you've resolved the error, but I have a few suggestions that may help.
Aggregator Code
I noticed that you are sending the callback in that code and passing in the data object, which is assigned to the err argument and the data argument will be undefined:
testing() {
data.volatile.modularVariables.read(
data.persistent.paths.srcRoot,
data.volatile.modularVariables.encoding.utf8,
data.volatile.modularVariables.readCB(data)
);
}
When passing in the callback function, you only need to specify the callback function name as below. NodeJS will call the callback with the appropriate err and data arguments. I believe this should resolve your problem.
testing() {
data.volatile.modularVariables.read(
data.persistent.paths.srcRoot,
data.volatile.modularVariables.encoding.utf8,
data.volatile.modularVariables.readCB
);
}
Variable Naming
One thing that could help others read your code and spot issues without needing to run the code is making sure you don't have variables with the same name. I believe you are attempting to reference your data object outside of the callback function, but in the scope of the callback function, data will be primarily referenced as the argument passed in (see scope). When I ran your code, the debugger showed me that data was undefined before applying the fix above. After, it showed me that data was an empty string.
For this, you can either change your data object to be named something like myData or cypherData and it will not conflict with any of your other variables/parameters.
Full Solution
var cypherData = {
persistent: {
states: {
//exempt for question clarity
},
paths: {
srcRoot: "./vortex/root.txt"
}
},
volatile: {
//much of the data exempt for question clarity
modularVariables: {
encoding: {
utf8: "utf8",
hex: "hex",
base64: "base64",
BIN: (data, encoding) => {
Buffer.from(data, encoding);
}
},
readCB: (err, data) => {
console.log(data);
if (err) {
console.log(
`%c${data.volatile.debug.debugStrings.errCodes.read}: Error reading from file`,
"color: red"
);
}
},
writeCB: (err) => {
if (err) {
console.log(
`%c${data.volatile.debug.debugStrings.errCodes.write}: Error writing to file`,
"color:red"
);
}
},
read: (file, encoding, cb) => {
fs.readFile(file, encoding, cb);
}, //cb = readCB READ file srcRoot, pass into root(rootHash,encoding)
write: (file, data, cb) => {
fs.writeFile(file, data, cb);
}, //cb = writeCB
checkInit: (symbol) => {
if (typeof symbol !== undefined) {
return symbol;
}
}
},
debug: {
functions: {
append: {
testProg: { program: { main: "system.node.upgrade" } }
}
},
debugStrings: {
errCodes: {
read: "AFTERNET ERR 000",
write: "AFTERNET ERR 001"
}
}
}
}
};
cypherData.volatile.modularVariables.read(
cypherData.persistent.paths.srcRoot,
cypherData.volatile.modularVariables.encoding.utf8,
cypherData.volatile.modularVariables.readCB
);
I have a project I inherited and I am writing up a bunch of tests before I refactor it, and I am having trouble testing a callback.
static createNew(data) {
ApiActions.post(
'/api/projects',
data,
(err, response) => {
// this is the callback I want to test gets triggered
if (!err) {
this.hideCreateNew();
}
}
);
}
ApiActions just builds and executes the ajax request, the third param being the callback.
my test so far:
import ApiActions from '#helpers/api';
jest.mock('#helpers/api');
...
it('should createNew', () => {
const callback = jest.fn((cb) => cb());
Actions.createNew({ data: [] }, callback);
expect(ApiActions.post).toHaveBeenCalledWith(
'/api/projects',
{ data: [] },
expect.any(Function)
);
expect(callback).toHaveBeenCalled();
});
You could refactor your code such that it becomes more testable by factoring the callback in createNew into a separate function and use that as the default callback, but allow other callbacks to be passed in as well.
I believe it should be something like this (may be slightly wrong, but it should illustrate the idea):
static createCallback(err, response) {
if (!err) {
this.hideCreateNew();
}
}
static createNew(data, callback) {
// default to createCallback
callback = callback || createCallback;
ApiActions.post(
'/api/projects',
data,
callback
);
}
Now you can test both createNew and createCallback independently whether the behavior is as expected.
In my Webpack configuration, I am using UglifyJsPlugin, which registers a callback to "normal-module-loader", where it sets context.minimize = true. I wanted to set it to false, so I wrote another plugin to my config:
plugins: [
...
{
apply: compiler => {
console.log('apply was called');
compiler.plugin('normal-module-loader', (context) => {
console.log('callback was invoked');
context.minimize = false;
});
}
}
But the callback never gets invoked and I don't know why. When I register callback to some other event (eg. "done"), it gets invoked, only this "normal-module-loader" doesn't.
Does anyone know what I'm doing wrong?
It seems you need to access Compilation instance first.
plugins: [
...
{
apply: compiler => {
console.log('apply was called');
compiler.plugin('compilation', compilation => {
compilation.plugin('normal-module-loader', (context) => {
console.log('callback was invoked');
context.minimize = false;
});
});
}
}
I am making an Alexa skill with AWS Lambda functions in NodeJS.
The app is throwing error when I call an Intent:
"errorMessage": "Exception: TypeError: object is not a function"
First, my app gets an event. If it's an Intent, it calls:
exports.handler = function (event, context) {
try {
...
else if (event.request.type === "IntentRequest") {
onIntent(
event.request,
event.session,
function intent_callback(sessionAttributes, speechletResponse) {
context.succeed(buildResponse(sessionAttributes, speechletResponse));
}
);
You can see the above passes a callback to onIntent(). It checks which Intent it is. Console.logging here shows the passed through callback as a function:
function onIntent(intentRequest, session, callback) {
if ("ItemIntent" === intentName) {
console.log(callback); // This is a function
getOrderResponse(intent, session, callback);
Yet, the type of the callback in getOrderResponse() somehow turns into an object? This would be why I'm getting that error, but I don't see how it's not a function type here. Why is it an object?
function getOrderResponse(callback) {
console.log('getOrderResponse', callback); // type = Object: { name: 'ItemIntent', slots: { Item: { name: 'Item' } } }
var card_title = config.data().CARD_TITLE;
var sessionAttributes = {},
speechOutput = 'So you want quick order',
shouldEndSession = false,
repromptText = 'Hello';
sessionAttributes = {
'speechOutput': repromptText,
'repromptText': repromptText,
'questions': 'some questions'
};
callback(sessionAttributes, buildSpeechletResponse(card_title, speechOutput, repromptText, shouldEndSession));
}
The callback will have to be the third parameter.
getOrderResponse(intent, session, callback); The first parameter you are sending is the the intent object.
function getOrderResponse(callback) {
should be
function getOrderResponse(intent, session, callback) {
I'm trying to get a history data from Pubnub.history(), store that data and update the views by using different controllers.
I've tried creating a service:
(function(){
'use strict';
angular.module('app')
.service('pubnubService', ['Pubnub',
pubnubService
]);
function pubnubService(Pubnub){
var history;
Pubnub.history({
channel : 'ParkFriend',
limit : 1,
callback : function(historyData) {
console.log("callback called");
history = historyData;
}
});
return {
getHistory : function() {
console.log("return from getHistory called");
return history;
}
};
}
})();
The problem is, getHistory() returns the data before Pubnub.history(). I need to make sure that history data is stored on history before returning it.
Since Pubnub.history is async, your getHistory function have to be an async function too.
Try the following:
function pubnubService(Pubnub) {
return {
getHistory: function(cb) { // cb is a callback function
Pubnub.history({
channel: 'ParkFriend',
limit: 1,
callback: function(historyData) {
console.log("callback called");
cb(historyData);
}
});
}
};
}
To use this service, you can't use it as a synchronous function (i.e., like var history = Pubnub.getHistory()), you need to pass a function as parameter to act like a callback.
Correct usage:
Pubnub.getHistory(function(history) { // here you have defined an anonym func as callback
console.log(history);
});