【问题标题】:Mongoose findOne callback not working猫鼬 findOne 回调不起作用
【发布时间】:2014-12-02 22:08:02
【问题描述】:

Mongoose findOne 函数调用什么也没做,我又遇到了麻烦。回调永远不会返回...

schema.js 文件:

var schemaSizeGroup = new Schema({
     sizeGroupId        :   {type: Number, required: true, index: true, index: { unique: true }}
    ,sizeGroupName      :   {type: String, required: true, trim: true, index: { unique: true }}
    ,sizeGroupValues    :   {type: String, required: true, trim: true }
    ,active             :   {type: Boolean, default: true }
}, { collection: 'sizegroup' }).index({sizeGroupId : 1});


module.exports ={
    SizeGroup       : mongoose.connection.model('SizeGroup', schemaSizeGroup),
}

index.js 文件:

findDocumentById : function(sGroupId, callback){
        winston.info("  Trying to select!");
        model.SizeGroup.findOne( {sizeGroupId : sGroupId} ,function(err, sGroup) {

            winston.info("  Select done:");
            winston.info(JSON.stringify(sGroup,null,2));

            if(!err) {
                if(!sGroup) {
                    callback(new Error("  No SizeObject Found for Id:" + sizeGroupId));
                } else { callback(null, sGroup); }
             }
             else {
                callback(err);
             }
        });
    }
}

使用 mongo 客户端选择数据可以很好地返回正确的数据:

db.sizegroup.find({sizeGroupId : 6});

使用 mongoose.set('debug', true) 时输出如下:

Mongoose: sizegroup.findOne({ sizeGroupId: 6 }) { fields: undefined } 

我有活跃的猫鼬连接,因为之前的所有插入语句都已成功。

我做错了吗?

【问题讨论】:

  • findOne 在这个完成后在不同的程序中完成时正在工作。程序中的所有插入都是使用 async.series 完成的,而且 mongoose.set('debug', true) 输出也证明了这一点。任何 cmets 为什么我无法访问同一程序中的数据....

标签: javascript node.js mongoose


【解决方案1】:

这是程序流程中的回调问题。明显的愚蠢案例....

【讨论】:

    猜你喜欢
    • 2016-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-16
    • 2019-02-13
    • 1970-01-01
    • 2019-09-10
    • 2018-09-03
    相关资源
    最近更新 更多