MongoError:无法规范化查询:BadValue> 1

时间:2014-05-20 22:41:38

标签: node.js mongoose

晚上好,

我一直忙于Mongoosejs + Node.js,目前我有以下错误。

[Login-01][ERROR] MongoError: Can't canonicalize query: BadValue >1 field in obj: { 
_events: {}, _posts: { save: [] }, _pres: { save: [] }, _doc: { _id: 
ObjectId('537a652e88d1ff281aa9ce95'), name: "Diamondo25", banResetDate: null, 
creationDate: new Date(1400530222052), female: null, isAdmin: false, __v: 0, password: 
"---", salt: "---" }, _maxListeners: 0, errors: null, isNew: false, $__: { fullPath:
 null, ownerDocument: null, activePaths: { stateNames: [ "require", "modify", "init",
 "default" ], states: { require: {}, modify: {}, init: { _id: true, name: true, 
banResetDate: true, creationDate: true, female: true, isAdmin: true, __v: true, 
password: true, salt: true }, default: {} }, paths: { _id: "init", name: "init", 
banResetDate: "init", creationDate: "init", female: "init", isAdmin:"init", __v: 
"init", password: "init", salt: "init" } }, scope: null, wasPopulated: false, 
populated: null, populate: null, _id: null, getters: {}, version: null, inserting: 
null, removing: null, adhocPaths: null, validationError: null, saveError: null, 
shardval: null, selected: null, strictMode: true } }

我无法在触发代码的简单变体中重现错误(在TCP数据包的回调中运行)。

function test() {
    var document = FindDocumentByCutoffId(Character, 1400622496);
    console.log(document);
    console.log(document.remove());
}
wait.launchFiber(test);
character回调中的

SetHandler与上面代码记录的对象相同。

PacketHandler.SetHandler(0x0017, function (pSocket, pReader) {
    var id = pReader.ReadUInt32();
    var character = FindDocumentByCutoffId(Character, id, {
        account: pSocket.account,
        worldId: pSocket.state.worldId
    });

    if (!character) {
        pSocket.Disconnect();
        return;
    }
    wait.forMethod(character, 'remove');
});

global.FindDocumentByCutoffId = function (pSchema, pDocumentId, pFilterAdditions) {
    var filter = pFilterAdditions || {};
    pDocumentId = pDocumentId.toString(16);

    // Get all rows
    var rows = wait.forMethod(pSchema, 'find', '_id', filter);

    for (var i = 0; i < rows.length; i++) {
        if (String(rows[i]._id).indexOf(pDocumentId) == 0) {
            return wait.forMethod(pSchema, 'findById', rows[i]._id);
        }
    }

    return null;
};

编辑等一下。为什么在错误中打印Account对象? O_O

1 个答案:

答案 0 :(得分:4)

如果您正在执行非法操作(例如尝试更新FindOne操作),则会发生此错误:

accounts.findOne({"user.email":options.user.email}, options, {upsert:false}, function(err, user) {
    if (err) callback(err);
    callback(null, 'updated thank you');
    });

当正确的方法是:

时,会引发异常
accounts.update({"user.email":options.user.email}, options, {upsert:false}, function(err, user) {
    if (err) callback(err);
    callback(null, 'updated thank you');
    });