3

This is the stack trace: This happens when I run the server.js file in my project. Also I couldn't find any explanation on the web. Can someone help with this error please?

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

These are the only codes that are using angenda and "s" < as a string

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 ✔ <[email protected]>',
            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
  • That means the code is trying to access the property s of an object variable that is not defined. If you want more help, show us the code where this happens. Is the source file something you wrote, or a dependency you're using?
    – Antoine
    Nov 4, 2015 at 7:28
  • @Antoine These are the codes only use the agenda node modules Nov 4, 2015 at 8:12

2 Answers 2

7

it works when .on('ready') function is present. Here is my example.

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();
});
2
  • This fixed my issue too. Interesting how there are so many example which don't use .on() Jul 8, 2016 at 9:58
  • The accepted answer isn't very helpful. I think most people know to use a working connection string. This answer, however, solved my problem.
    – Tim Hardy
    Mar 6, 2017 at 4:42
3

In agenda.js, at line if ( this._mdb.s.topology.connections().length === 0 ), this._mdb is undefined.

The method Agent.prototype.database assigns to _mdb the returned db object from MongoClient.connect. So my guess is that the connect operation fails. Are you sure of your connection string?

8
  • Ok I have chaged the connection string to 'localhost:27017/test' still gives me an error. The mongodb says listening to port 27017 Nov 4, 2015 at 8:35
  • Mongo connection strings must be in the form mongo://[server]:[port]/[dbname]. the part after the host/port is the name of the database to connect to. Make sure it's correctl written, and check the permissions too.
    – Antoine
    Nov 4, 2015 at 8:38
  • OK it still gives me an error the hosts that are tried are: localhost:27017/test, 127.0.0.1:27017/test, localhost:52332/test <-- when the mogos shell started. Still gives me an error. Nov 4, 2015 at 9:16
  • Using the same connection string, can you connect to the db with the shell or a gui tool like robomongo?
    – Antoine
    Nov 4, 2015 at 9:49
  • 1
    Mongo shell does not exactly use the connection string, but you can see this: docs.mongodb.org/manual/tutorial/…
    – Antoine
    Nov 4, 2015 at 10:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.