0

我有以下原型

{
    "_id" : "Nwva",
    "cat" : {
        "_id" : ObjectId("4ffee943e4b08a66fd8e84a1"),
        "slug" : "fun"
    },
    "imp" : false,
    "int" : {
        "comm" : 0,
        "fblk" : 1,
        "fbsh" : 0
    }
}

以及下面的map reduce函数:

var mapFunction1 = function() {
    if (!this.int || (typeof this.int != 'object')) {
        this.int = {};
    }
    var lk = this.int.lk;
    var dlk = this.int.dlk;
    .....
    .....
};

当我有一个没有字段"int"的原型时,我遇到了错误:

JavaScript 执行失败:map reduce failed:{ "errmsg" : "exception: JavaScript execution failed: TypeError: Cannot read property 'lk' of undefined near '= this.int.lk; \t\tvar dlk = this.int.dlk '(第 5 行)”、“代码”:16722、“确定”:0 }

我已经尝试了一切,"this.int = {};"总是分配,没有。

当 this.int 未定义以避免异常时,我尝试了几种分配空对象的方法,但均未成功。

这个变量在 map 函数范围内是只读的吗?

我该如何解决这个问题?

附加信息

这里我们有一个简单的测试:

db.contenido.insert( { "_id": "Flor" });
db.contenido.find({ "_id": "Flor" } );
{ "_id" : "Flor" }

db.contenido.mapReduce(function() { var lk = this.int.lk;}, function() {}, { query :  { "_id" : "Flor" }, out : { inline : 1 },verbose: 1 });

Tue Jul 23 17:39:53.114 JavaScript execution failed: map reduce failed:{
    "errmsg" : "exception: JavaScript execution failed: TypeError: Cannot read property 'lk' of undefined near '= this.int.lk; printjsononeline( this.int' ",
    "code" : 16722,
    "ok" : 0
} at src/mongo/shell/collection.js:L970

db.contenido.mapReduce(function() { if (!this.int || (typeof this.int != 'object')) {this.int = {};} var lk = this.int.lk;}, function() {}, { query :  { "_id" : "Flor" }, out : { inline : 1 },verbose: 1 });

Tue Jul 23 17:41:04.035 JavaScript execution failed: map reduce failed:{
    "errmsg" : "exception: JavaScript execution failed: TypeError: Cannot read property 'lk' of undefined near '= this.int.lk;}' ",
    "code" : 16722,
    "ok" : 0
} at src/mongo/shell/collection.js:L970


db.contenido.mapReduce(function() { this.int = {}; var lk = this.int.lk;}, function() {}, { query :  { "_id" : "Flor" }, out : { inline : 1 },verbose: 1 });


Tue Jul 23 17:41:21.926 JavaScript execution failed: map reduce failed:{
    "errmsg" : "exception: JavaScript execution failed: TypeError: Cannot read property 'lk' of undefined near '= this.int.lk;}' ",
    "code" : 16722,
    "ok" : 0
} at src/mongo/shell/collection.js:L970
4

0 回答 0