Hello, I’m starting to learn Meteor. Help written in the data base of the letter. I get an error when I want to do it.
W20160208-17:43:11.724(3)? (STDERR) throw new Error("Meteor code must always run within a Fiber. " +
W20160208-17:43:11.725(3)? (STDERR) ^
W20160208-17:43:11.876(3)? (STDERR) Error: Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment.
My code.
function generateUUID() {
var d = new Date().getTime();
var uuid = 'xxx-xxx-xxx'.replace(/[xy]/g, function(c) {
var r = (d + Math.random()*16)%16 | 0;
d = Math.floor(d/16);
return (c=='x' ? r : (r&0x3|0x8)).toString(16);
});
return uuid;
};
var Imap = Meteor.npmRequire('imap')
var MailParser = Meteor.npmRequire("mailparser").MailParser
var imap = new Imap({
user: 'zzzzzzz',
password: 'zzzzzzzzzzz',
host: 'zzzzzzzzzzz',
port: 993,
tls: true,
tlsOptions: { rejectUnauthorized: false }
});
function openInbox(cb) {
imap.openBox('INBOX', true, cb);
}
imap.once('ready', function() {
openInbox(function(err, box) {
console.log("open")
if (err) throw err;
imap.search(['UNSEEN'], function(err, results) {
/*imap.search([ 'ALL', ['SINCE', 'May 20, 2010'] ], function(err, results) {*/
if (err) throw err;
var f = imap.fetch(results, { bodies: '' });
f.on('message', function(msg, seqno) {
var mailparser = new MailParser({
showAttachmentLinks: false,
streamAttachments: false
})
msg.on('body', function(stream, info) {
stream.pipe( mailparser );
mailparser.on("end",function( mail){
/* THIS PROBLEM */
Imaps.insert({
id: generateUUID(),
from: mail.from,
subject: mail.subject,
html: mail.html,
status: 'open',
created_at: mail.date,
updated_at: new Date(),
read: 0
});
});
});
msg.once('end', function() {
console.log(seqno + 'Finished');
});
});
f.once('error', function(err) {
console.log('Fetch error: ' + err);
});
f.once('end', function() {
console.log('Done fetching all messages!');
imap.end();
});
});
});
});
imap.once('error', function(err) {
console.log(err);
});
imap.once('end', function() {
console.log('Connection ended');
});
imap.connect();