Javascript MongoDb映射减少异常
我有以下原型:Javascript MongoDb映射减少异常,javascript,mongodb,mapreduce,Javascript,Mongodb,Mapreduce,我有以下原型: { "_id" : "Nwva", "cat" : { "_id" : ObjectId("4ffee943e4b08a66fd8e84a1"), "slug" : "fun" }, "imp" : false, "int" : { "comm" : 0, "fblk" : 1, "fbsh" : 0 } } 以及以下地图缩小功能: var mapFu
{
"_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执行失败:映射减少失败:{“errmsg”:
异常:JavaScript执行失败:TypeError:无法读取
在'=this.int.lk;\t\tvar dlk'附近未定义的属性'lk'=
this.int.dlk“(第5行)”,“代码”:16722,“确定”: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
可能在应用函数之前“this”被冻结了……您试图从何处执行此代码?我刚刚尝试了一个简单的版本,它很有效。@WiredPairie我添加了一个简单的测试作为例子。你能在映射函数上设置一个this属性吗?你有没有尝试过定义一个新的变量,比如
var=this在映射函数的开头进行编码>,并对进行所有修改。我的数据显然太简单了,无法捕捉到您的具体错误。无法将数据设置到文档中(由this
表示)。你只需要复制数据