我正在使用app使用node-imap,並且我一直在處理錯誤或缺少錯誤時遇到了困難。我有以下功能:錯誤與錯誤拒絕與藍鳥。解決就好了。錯誤似乎崩潰了應用程序
////imap-helpers.js
function searchBox(searchTerm, bodies){
return new Promise(function(resolve, reject){
searchTerm = searchTerm || ['ALL']
bodies = bodies || ['HEADER.FIELDS (FROM TO CC BCC SUBJECT)','TEXT']
imap.search(searchTerm, function(err, results){
if (err) reject(err)
var messages = [], newMessage = {}, count=0;
var f = imap.fetch(results, {
bodies: bodies,
struct: true
});
f.on('message', function(msg) {
newMessage = {}
msg.on('body', function(stream, info) {
//When we get a message, append the header to header, text to body.
stream.on('data', function(chunk){
if (info.which !== 'TEXT')
newMessage.rawHeader += chunk.toString('utf8')
else
newMessage.body += chunk.toString('utf8')
})
//When stream is done, strip the unparsable characters at the beginning before parsing.
//NOTE: I'm not actually sure what these unparseable characters actually are
//but this all works kosher.
stream.once('end', function() {
if (info.which !== 'TEXT'){
newMessage.rawHeader = newMessage.rawHeader.replace(/^undefined/, '')
newMessage.header = Imap.parseHeader(newMessage.rawHeader)
}
if(newMessage.body)
newMessage.body = newMessage.body.replace(/^undefined/, '')
})
})
msg.once('attributes', function(attrs) {
newMessage.attrs = attrs
})
msg.once('end', function() {
messages[count] = _.omit(newMessage, 'rawHeader')
count++
})
})
f.on('error', function(e){
reject(e)
})
f.once('end', function(){
resolve(messages)
})
})
})
}
exports.getByUID = function(boxName, uid){
boxName = boxName || 'INBOX'
var searchTerm = ['ALL',['UID',uid]]
return imap.openBoxAsync(boxName, false).then(function(){
return searchBox(searchTerm, ['HEADER.FIELDS (FROM TO CC BCC SUBJECT)']).then(function(messages){
return messages
}).catch(function(e){
throw e
})
}).catch(function(e){
return e
})
}
然後我可以在文件中調用它。
//imap-controller.js - severely amended version
getOne: function(req, res){
methods.getByUID('INBOX',req.params.uid).then(function(response){
res.json(response)
}).catch(function(e){
res.json({error: e.message})
})
}
但問題是我沒有運氣返回到控制器的錯誤。它只是不斷地打破了
Unhandled rejection Error: Nothing to fetch
at Connection._fetch (/Users/dkranec/github/imap/node_modules/imap/lib/Connection.js:778:11)
at Connection.fetch (/Users/dkranec/github/imap/node_modules/imap/lib/Connection.js:771:15)
at Connection.tryCatcher (/Users/dkranec/github/imap/node_modules/bluebird/js/main/util.js:26:23)
at Connection.ret [as fetchAsync] (eval at <anonymous> (/Users/dkranec/github/imap/node_modules/bluebird/js/main/promisify.js:163:12), <anonymous>:15:23)
at /Users/dkranec/github/imap/app.js:34:12
at tryCatcher (/Users/dkranec/github/imap/node_modules/bluebird/js/main/util.js:26:23)
at Promise._settlePromiseFromHandler (/Users/dkranec/github/imap/node_modules/bluebird/js/main/promise.js:503:31)
at Promise._settlePromiseAt (/Users/dkranec/github/imap/node_modules/bluebird/js/main/promise.js:577:18)
at Promise._settlePromises (/Users/dkranec/github/imap/node_modules/bluebird/js/main/promise.js:693:14)
at Async._drainQueue (/Users/dkranec/github/imap/node_modules/bluebird/js/main/async.js:123:16)
at Async._drainQueues (/Users/dkranec/github/imap/node_modules/bluebird/js/main/async.js:133:10)
at Immediate.Async.drainQueues [as _onImmediate] (/Users/dkranec/github/imap/node_modules/bluebird/js/main/async.js:15:14)
at processImmediate [as _immediateCallback] (timers.js:371:17)
請看到相關reddit的文章中,我對這個話題該功能的應用程序我做了也有問題的小版本所做這裏,如果你認爲你可以與here幫助程序。基本上同一個問題的另一個版本,但捕捉錯誤,但沒有放出imap.fetch發射器成功。我贏不了!
井,至少'如果(ERR)拒絕(ERR)'應該是'如果(ERR){拒絕(ERR) ;返回; }' –
什麼是'imap.openBoxAsync'?這是你寫的一個函數嗎?如果是這樣,你需要顯示它。 –
你確定'var newMessage ...'在正確的範圍內嗎?正如所寫的,每個'f.on('message'...'將導致先前的'newMessage'被覆蓋,並且流只會將它們的塊貢獻給最後創建的'newMessage' - 這至少可能是錯誤的!我會認爲'var newMessage ...'應該在'f.on('message'...'作用域中 –