“TypeError:无法读取未定义的属性'”。在agenda.js中这是什么意思?

时间:2015-11-04 07:12:36

标签: javascript node.js visual-studio-2015

这是堆栈跟踪:当我在项目中运行server.js文件时会发生这种情况。我也无法在网上找到任何解释。有人可以帮忙解决这个错误吗?

E:\users\ashane\documents\visual studio 2015\Projects\ShowTracker\ShowTracker\node_modules\agenda\lib\agenda.js:341
  if ( this._mdb.s.topology.connections().length === 0 ) {                ^

TypeError: Cannot read property 's' of undefined
    at Agenda._findAndLockNextJob (E:\users\ashane\documents\visual studio 2015\Projects\ShowTracker\ShowTracker\node_modules\agenda\lib\agenda.js:341:17) at jobQueueFilling (E:\users\ashane\documents\visual studio 2015\Projects\ShowTracker\ShowTracker\node_modules\agenda\lib\agenda.js:420:10) at processJobs (E:\users\ashane\documents\visual studio 2015\Projects\ShowTracker\ShowTracker\node_modules\agenda\lib\agenda.js:404:7)

    at doNTCallback0 (node.js:417:9)
    at process._tickCallback (node.js:346:13)
    at Function.Module.runMain (module.js:469:11)
    at startup (node.js:134:18)
    at node.js:961:3

这些是使用angenda和“s”<的唯一代码。作为一个字符串

var mongoConnectionString = "mongodb://127.0.0.1/agenda";
var agenda = require('agenda')({ db: { address: mongoConnectionString } });
var sugar = require('sugar');
var nodemailer = require('nodemailer');

// define an agenda job called send email alert.

agenda.define('send email alert', function (job, done) {
    // what should happen when send email alert job is dispatched. 
    // When this job runs, name of the show will be passed in as an optional data object. 
    // Since we are not storing the entire user document in subscribers array (only references), 
    // we have to use Mongoose’s populate method.
    Show.findOne({ name: job.attrs.data }).populate('subscribers').exec(function (err, show) {
        var emails = show.subscribers.map(function (user) {
            return user.email;
        });

        var upcomingEpisode = show.episodes.filter(function (episode) {
            return new Date(episode.firstAired) > new Date();
        })[0];

        var smtpTransport = nodemailer.createTransport('SMTP', {
            service: 'SendGrid',
            auth: { user: 'hslogin', pass: 'hspassword00' }
        });

        // standard Nodemailer boilerplate for sending emails.
        var mailOptions = {
            from: 'Fred Foo ✔ <foo@blurdybloop.com>',
            to: emails.join(','),
            subject: show.name + ' is starting soon!',
            text: show.name + ' starts in less than 2 hours on ' + show.network + '.\n\n' +
        'Episode ' + upcomingEpisode.episodeNumber + ' Overview\n\n' + upcomingEpisode.overview
        };

        smtpTransport.sendMail(mailOptions, function (error, response) {
            console.log('Message sent: ' + response.message);
            smtpTransport.close();
            done();
        });
    });
});

agenda.start();

agenda.on('start', function (job) {
    console.log("Job %s starting", job.attrs.name);
});

agenda.on('complete', function (job) {
    console.log("Job %s finished", job.attrs.name);
});

2 个答案:

答案 0 :(得分:7)

当.on(&#39; ready&#39;)功能存在时,它会起作用。这是我的例子。

var Agenda = require('agenda');
var agenda = new Agenda({db: { address: 'localhost:27017/servicebox-dev'}}); 
matcher.matchProviders(agenda);
agenda.on('ready', function() {
    agenda.every('5 seconds', 'match providers'); 
    agenda.start();
});

答案 1 :(得分:2)

在agenda.js中,在第tf workspaces行,if ( this._mdb.s.topology.connections().length === 0 )未定义。

方法this._mdbAgent.prototype.database分配给_mdb返回的db对象。所以我的猜测是连接操作失败了。你确定你的连接字符串吗?