私は次のプロトタイプを持っています:
{
"_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 に失敗しました:{ "errmsg" : "例外: JavaScript の実行に失敗しました: TypeError: '= this.int.lk 付近の未定義のプロパティ 'lk' を読み取れません; \t\tvar dlk = this.int.dlk ' (5 行目)", "コード": 16722, "OK": 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