TypeError:无法读取null的属性“电子邮件”

时间:2016-05-06 07:28:40

标签: node.js mongodb

这是第54行,我收到错误:

if (docr.emails) {

以下是我原始代码的其余部分:

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 host = meeting.host;

    db.collection('users').findOne({'_id': new ObjectId(host)}, function(err, docr) {
        var emails = [];
        if (docr.emails) {
            docr.emails.forEach(function(e) {
                emails.push(e.email + (e.primary ? '(P)' : ''));
            });
        }
        var email = emails.join(', ');


        if (utils.toSkipEmail(email)) {
            callback();
        } else {
        var cursor = db.collection('meetings').find({
            'email': {'$regex': 'abc', '$options': 'i' }
        }); 
        }   


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

1 个答案:

答案 0 :(得分:0)

尝试以下方法:

myApp.directive('mypDirective',function(){
    return {
      restrict:'E',
      scope: {
        myarg: '='
      },
      controller: 'DirectiveCtrl',
      controllerAs: 'directiveCtrl',
      bindToController: true,
      template: '{{directiveCtrl.text}}'
    };
  }
);