简体   繁体   中英

TypeError: Cannot read property 'toString' of undefined - why?

Here is the line (50) where this is happening:

var meetingId = meeting._id.toString(),

And here is the full, relevant code:

var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var ObjectId = require('mongodb').ObjectID;
var config = require('./config'),
    xlsx = require('./xlsx'),
    utils = require('./utils'),
    _ = require('lodash'),
    url = config.DB_URL;

var meetings = [];

function findNumberOfNotesByMeeting(db, meeting, callback) {
    var meetingId = meeting._id.toString(),
        meetingName = meeting.name.displayValue,
        attendees = meeting.attendees;
        host = meeting.host;

    var count = 1, pending = 0, accepted = 0;
    console.log("==== Meeting: " + meetingName + '====');
    _.each(attendees, function(item) {
      console.log(count++ + ': ' + item.email + ' (' + item.invitationStatus + ')');
      if (item.invitationStatus == 'pending') { pending++; }
      else if (item.invitationStatus == 'accepted') { accepted++; }
    });
    console.log("*** " + attendees.length + ", " + pending + "," + accepted);

    db.collection('users').findOne({'_id': new ObjectId(host)}, function(err, doc) {
        var emails = [];
        if (doc.emails) {
            doc.emails.forEach(function(e) {
                emails.push(e.email + (e.primary ? '(P)' : ''));
            });
        }
        var email = emails.join(', ');
        if (utils.toSkipEmail(email)) {
            callback();
        } else {
            db.collection('notes').find({ 'meetingId': meetingId }).count(function(err, count) {
                if (count != 0) {
                    console.log(meetingName + ': ' + count + ',' + attendees.length + ' (' + email + ')');
                    meetings.push([ meetingName, count, email, attendees.length, pending, accepted ]);
                }
                callback();
            });
        }
    });
}

function findMeetings(db, meeting, callback) {  
    var meetingId = meeting._id.toString(),
        host = meeting.host;
    db.collection('users').findOne({'_id': new ObjectId(host)}, function(err, doc) {
        var emails = [];
        if (!err && doc && doc.emails) {
            doc.emails.forEach(function(e) {
                emails.push(e.email + (e.primary ? '(P)' : ''));
            });
        }

        var email = emails.join(', ');

        if (utils.toSkipEmail(email)) {
            callback();
        } else {
            db.collection('notes').find({ 'meetingId': meetingId }).count(function(err, count) {
                if (count != 0) {
                    var cursor = db.collection('meetings').find({
                    'email': {'$regex': 'agu', '$options': 'i' }
                    });
                }
                callback();
        }); 

        }   


    cursor.count(function(err, count) {
        console.log('count: ' + count);
        var cnt = 0;
        cursor.each(function(err, doc) {
            assert.equal(err, null);
            if (doc != null) {
                findNumberOfNotesByMeeting(db, doc, function() {
                    cnt++;
                    if (cnt >= count) { callback(); }
                });
            }
        });
    });
    });
}


MongoClient.connect(url, function(err, db) {
    assert.equal(null, err);
    findMeetings(db, function() {
        var newMeetings = meetings.sort(function(m1, m2) { return m2[1] - m1[1]; });
        newMeetings.splice(0, 0, [ 'Meeting Name', 'Number of Notes', 'Emails' ]);
        xlsx.writeXLSX(newMeetings, config.xlsxFileNameMeetings);
        db.close();
    });
});

As you can see, the meeting variable (which I am almost 100% sure is the problem, not the _id property) is passed in just fine as a parameter to the earlier function findNumberOfNotesByMeeting. I have found some information here on SO about the fact that my new function may be asynchronous and needs a callback, but I've attempted to do this and am not sure how to get it to work, or even if this is the right fix for my code.

You're not passing the meeting object to findMeetings , which is expecting it as a second parameter. Instead of getting the meeting object, the function receives the callback function in its place, so trying to do meeting._id is undefined.

In fact, what is the purpose of the findMeetings function? It's name indicates it can either find all meetings in the database, or all meetings with a specific id. You're calling it without a meeting id indicating you might be trying to find all meetings, but its implementation takes a meeting object. You need to clear that up first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM