我有一个要在地图操作中使用的函数,该函数将局部变量与从地图返回的文档进行比较。我直接尝试过,并且将其作为IIFE进行了尝试,并且两种方式都在抱怨地图找不到名为boatNumber
的变量:
var mymap = (function() {
var boatNumber = $('#add_result_boat_number').val();
return function(doc) {
if (doc.boatnumber == boatNumber) {
emit(doc._id, {boatClass: doc.boatClass, p1name: doc.p1name, p2name: doc.p2name});
}
}
})();
ro.pdb.query(mymap).then(function(response) {
var results = response.rows;
// do stuff later
}).catch(function(error) {
ro.reporter(error);
});
如果我将boatNumber
中的常量替换为==
,它将正常工作。那么如何使boatNumber
在map函数中被识别?
更新
Error messages are:
The user's map/reduce function threw an uncaught error.
You can debug this error by doing:
myDatabase.on('error', function (err) { debugger; });
Please double-check your map/reduce function.
跟着
ReferenceError: "boatNumber is not defined"
anonymous file:///Users/ptomblin/Dropbox/KayakResults/js/pouchdb-7.1.1.min.js line 7 > Function:5
Zn file:///Users/ptomblin/Dropbox/KayakResults/js/pouchdb-7.1.1.min.js:7
n file:///Users/ptomblin/Dropbox/KayakResults/js/pouchdb-7.1.1.min.js:7
e file:///Users/ptomblin/Dropbox/KayakResults/js/pouchdb-7.1.1.min.js:7
答案 0 :(得分:1)
传递给query
的函数被字符串化并发送到数据库。然后在那里进行评估。因此,您不能从外部访问任何东西,它必须是纯净的,换句话说,您只能访问传递的doc
和builtin methods(是的,文档中确实应该提到)。>
看起来,您可以通过常规的find()
查询简单地完成您想做的事情:
var boatNumber = $('#add_result_boat_number').val();
ro.pdb.find({
selector: {
boatnumber: boatNumber,
},
fields: ["boatClass", "p1name", "p2name"]
}).then(...)