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

luaexgnf  于 2023-02-15  发布在  Go
关注(0)|答案(1)|浏览(146)

这是发生这种情况的行(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();
    });
});

如您所见,meeting变量(我几乎100%肯定是问题所在,而不是_id属性)作为参数传递给前面的函数findNumberOfNotesByMeeting。我在SO上找到了一些信息,关于我的新函数可能是异步的,需要回调,但我已经尝试过了,不知道如何让它工作,或者这是否是对我代码的正确修复。

sirbozc5

sirbozc51#

您没有将meeting对象传递给findMeetings,因为findMeetings需要将其作为第二个参数。该函数没有获取meeting对象,而是在其位置接收回调函数,因此尝试执行meeting._id是未定义的。
实际上,findMeetings函数的用途是什么?它的名称表明它可以查找数据库中的所有会议,或者查找具有特定id的所有会议。您调用它时没有会议id,表明您可能试图查找所有会议,但它的实现需要一个会议对象。您需要首先清除该对象。

相关问题