"拓扑被破坏" Mongoose中更新功能的错误

时间:2016-10-14 17:12:19

标签: mongodb mongoose

尝试运行更新命令时遇到{ [MongoError: topology was destroyed] name: 'MongoError', message: 'topology was destroyed' }错误。我对这个错误的理解是它是由数据库连接被丢弃引起的,但这不是有意义的,因为它只是对模型的更新调用,对吗?这是我的代码的相关部分:

/*Connect to the Database*/
var options = {
  server: { socketOptions: { keepAlive: 1, connectTimeoutMS: 60000 } },
  replset: { socketOptions: { keepAlive: 1, connectTimeoutMS: 60000 } }
};
var mongoose = require('mongoose'),
    ObjectID = mongoose.Types.ObjectId;

mongoose.connect('remote.database', options, function(err) {

    if (err) console.log(err);
});

// Schema for single user
var user_schema = mongoose.Schema({
    //Username, password information
    username: String,
    email: String,
    password: String,
    name: String,
    session_ids: [String],
    session_id_times: [Number],

    //Khan Academy token, secret for accessing tree of knowledge
    khan_token: String,
    khan_secret: String,

    //Callback is for authenticating with khan
    khan_callback_id: String,

    //Proficiencies (data from khan API)
    proficiencies: [String],
    additional_skills: [String],

    //general recommended skills to learn next
    gen_skill_recs: [String]
});

var User = mongoose.model("user_schema", user_schema);

/*
    Logins a user without a password after they have authenticated with
    Khan. Is only called as a callback to AM.updateAuthentication which
    sets khan-callback_id to an empty string so that this only works after
    logging on and authenticating with Khan.
*/
exports.loginAfterAuthentication = function(user, callback){
    User.findOne({username: user}, function(err, o){
        if(err){
            callback('server-error');
        }else if(o){
            var ids = [];
            o.session_id_times.forEach(function(el, i) {
                if(d.getTime()-el > 24*60*60*14)
                    ids.push(i);
            });
            o.session_id_times = o.session_id_times.filter(function(el, i) {return ids.indexOf(i) == -1});
            o.session_ids = o.session_ids.filter(function(el, i) {return ids.indexOf(i) == -1});
            var session_id = generateSalt();
            var session_id_time = d.getTime();
            o.session_ids.push(session_id);
            o.session_id_times.push(session_id_time);
            User.findOneAndUpdate({username: user}, {session_ids: o.session_ids, session_id_times: o.session_id_times}, function(err, o){
                if(err)
                    callback('user-not-found');
                else{
                    callback(null, {'username': user, 'session_id': session_id});
                    //somewhat time consuming call
                    API.getUserExercises(o.khan_token, o.khan_secret).then(
                        function(result){
                            var addSkills = result;
                            // call to update proficiencies. Somewhat time consuming
                            API.getUserInfo(o.khan_token, o.khan_secret).then(
                                function(result){
                                    var profEx = result.proficient_exercises;
                                    function isntProficient(el){
                                        return profEx.indexOf(el) == -1;
                                    }
                                    // Updates proficiencies and removes additional_skills that have been added to
                                    // proficiencies
                                    console.log(user);
                                    console.log(profEx);
                                    console.log(addSkills.filter(isntProficient));
                                    User.update({username: user},{proficiencies: profEx,
                                        additional_skills: addSkills.filter(isntProficient)}, function(err) {
                                            if(err) callback(err); //This is where this error is triggered
                                            else updateClonesStatuses(user, function(){});
                                        });
                                }, function(error){
                                    callback('must-reauthenticate', o);
                                });
                        }, function(error) {
                            // if API.getUserInfo() throws an error the user's token
                            // and secret are depreciated and they must reauthenticate
                            // with Khan. Should happen very rarely
                            callback('must-reauthenticate', o);
                        });
                        }
                    });
        } else {
            callback('user-not-found');
        }
    });
}

user是用户的名称,profExaddSkills.filter(isntProficient)都是合理的值。我是否误解了如何以某种方式连接到mongo?

1 个答案:

答案 0 :(得分:0)

在mongoose"拓扑破坏"当您的节点服务器丢失与mongodb服务器的TCP连接并且已超过其最大重试连接限制时,会发生错误。在这种情况下,与mongodb的节点js连接变得理想,并且不能对DB执行任何操作,并且当任何请求尝试连接mongodb(没有节点服务器重启)时,它将抛出相同的错误。