Ich versuche, einige Code zu bereinigen, erfordert ich in einem hapi Server index.js
..ein Objektliteral mit Funktionen
Es gibt einige Code, der einrichtet Apples Push-Netzwerk gefunden, legt sich dann über eine Variable auf der server.apnConnection
Mein Problem ist, wenn ich Funktionen aufrufen, die ich eingerichtet habe .. sie fehlen/nicht da.
index.js
server = require('./config/server/hapi.js')(config, process.env.NODE_URL);
server.apnConnection = require('./config/server/applepush.js');
server.apnConnection.note("test");
applepush.js
'use strict';
var apn = require('apn');
var Path = require('path');
module.exports = function() {
var options = {
gateway: 'www.mapple.com',
errorCallback: function(errorNum, notification){
console.log('Error is: %s', errorNum);
console.log('Note ' + JSON.stringify(notification));
},
cert: process.env.APPLE_CERT || Path.join(config.rootPath, '../cert.pem'),
key: process.env.APPLE_KEY || Path.join(config.rootPath, '../key.pem'),
enhanced: true,
production: false,
cacheLength: 100,
port: 2195
};
var apnConn = new apn.Connection(options);
apnConn.on('connected',function(){
console.log('connected to apn');
});
apnConn.on('transmitted', function(notification, device) {
console.log('Notification transmitted to:' + device.token.toString('hex'));
});
apnConn.on('transmissionError', function(errCode, notification, device) {
console.error('Notification caused error: ' + errCode + ' for device ', device, notification);
if (errCode === 8) {
console.log('A error code of 8 indicates that the device token is invalid. This could be for a number of reasons - are you using the correct environment? i.e. Production vs. Sandbox');
}
});
apnConn.on('timeout', function() {
console.log('APNS Connection Timeout');
});
apnConn.on('disconnected', function() {
console.log('Disconnected from APNS');
});
apnConn.on('socketError', console.error);
return {
init: apnConn,
note: function (obj) {
var note = new apn.Notification();
note.setAlertText(obj.alert);
note.badge = 1;
return note;
}
}
};
Fehler:
server.apnConnection.note("test");
^
TypeError: undefined is not a function
at Object.<anonymous> (~/server/index.js:60:22)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.runMain [as _onTimeout] (module.js:501:10)
at Timer.listOnTimeout (timers.js:110:15)
ah, so von jetzt habe ich nur die Funktion zurückgegeben, die das Objekt, das ich erwartet hatte .. ApnConnection sollte nach 'Server', ja. – Erik
stellt sich heraus, ich brauche 'require (... Pfad-zu-js-Datei) (Config)', aber anders als das es funktioniert hat! Vielen Dank! – Erik