2016-05-22 60 views
0

我試圖清理一些代碼,我在一個高致病性禽流感服務器index.js發現..需要一個對象文本與功能

有一些代碼,建立了蘋果的推送網絡,然後通過一個可變的重視本身的server.apnConnection

我的問題是,當我嘗試調用我設置的功能..他們缺少/不存在。

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; 
     } 
    } 
}; 

錯誤:

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) 

回答

1

applepush.js出口的功能,而不是一個對象。您正在查找的對象由導出的函數返回。

你或許應該寫類似:

server = require('./config/server/hapi.js')(config, process.env.NODE_URL); 
// Note the function call. 
server.apnConnection = require('./config/server/applepush.js')(); 
server.apnConnection.note("test"); 

另一種解決方案是修改applepush.js,以便它返回一個對象,而不是一個功能。然而,這個函數的目標似乎是建立一個服務器,並且你可能不希望這個服務器在模塊加載後立即建立。

+0

啊,所以到現在爲止我只返回了我期待的對象返回的函數.. apnConnection應該在'server'後面設置,是的。 – Erik

+0

事實證明我需要'需要(...路徑到js文件)(配置)',但除此之外它的工作! 謝謝! – Erik