IIFE returns map函数,map不能用var
IIFE returns map function, map can't use var
我有一个要在地图操作中使用的函数,它将局部变量与从地图返回的文档进行比较。我直接试了,也试了IIFE,两种方式都在抱怨map找不到名为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
传递给 query
的函数被字符串化并发送到数据库。然后在那里进行评估。因此你不能从外部访问任何东西,它必须是纯粹的,所以换句话说你只能访问传递的 doc
和 builtin methods (是的,文档应该提到这一点)。
看起来,你想做的事情可以通过常规 find()
查询轻松完成:
var boatNumber = $('#add_result_boat_number').val();
ro.pdb.find({
selector: {
boatnumber: boatNumber,
},
fields: ["boatClass", "p1name", "p2name"]
}).then(...)
我有一个要在地图操作中使用的函数,它将局部变量与从地图返回的文档进行比较。我直接试了,也试了IIFE,两种方式都在抱怨map找不到名为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
传递给 query
的函数被字符串化并发送到数据库。然后在那里进行评估。因此你不能从外部访问任何东西,它必须是纯粹的,所以换句话说你只能访问传递的 doc
和 builtin methods (是的,文档应该提到这一点)。
看起来,你想做的事情可以通过常规 find()
查询轻松完成:
var boatNumber = $('#add_result_boat_number').val();
ro.pdb.find({
selector: {
boatnumber: boatNumber,
},
fields: ["boatClass", "p1name", "p2name"]
}).then(...)