我有以下 原型 :
{
"_id" : "Nwva",
"cat" : {
"_id" : ObjectId("4ffee943e4b08a66fd8e84a1"),
"slug" : "fun"
},
"imp" : false,
"int" : {
"comm" : 0,
"fblk" : 1,
"fbsh" : 0
}
}
以下 地图缩小 功能:
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失败:{“errmsg”: “异常:JavaScript执行失败:TypeError:无法读取 属性'lk'未定义在'= this.int.lk; \ t \ tvar dlk = this.int.dlk'(第5行)“,”代码“:16722,”ok“:0}
我已尝试过所有内容,始终分配"this.int = {};"
。
在未定义this.int以避免异常时,我尝试了多种方法来分配空对象失败。
这个变量在地图功能范围内是只读的吗?
我该如何解决这个问题?
其他信息
这里有一个简单的测试:
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