TypeError:无法读取未定义的属性'toString' - 为什么?

时间:2016-05-06 23:52:28

标签: javascript node.js mongodb

以下是发生这种情况的第50行:

var meetingId = meeting._id.toString(),

以下是完整的相关代码:

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();
    });
});

正如您所看到的,会议变量(我几乎100%确定是问题,而不是_id属性)作为参数传递给早期函数findNumberOfNotesByMeeting。我在这里找到了一些关于我的新函数可能是异步并且需要回调这一事实的信息,但我试图这样做并且不确定如何使它工作,或者即使这是正确的修复我的代码。

1 个答案:

答案 0 :(得分:0)

您未将meeting对象传递给findMeetings,而meeting期望将其作为第二个参数。该函数不是获取meeting._id对象,而是在其位置接收回调函数,因此尝试findMeetings未定义。

事实上,{{1}}功能的目的是什么?它的名称表示它可以查找数据库中的所有会议,也可以查找具有特定ID的所有会议。您在没有会议ID的情况下调用它,表明您可能正在尝试查找所有会议,但其实现需要会议对象。你需要先清除它。