Javascript MongoDB和Mongoose:$where回调中的ES6语法问题
我正在开发Node.JS(6.6.0)应用程序,它使用MongoDB(3.2.10)作为数据库和Mongoose(1.4.X)库。 下面的代码应在MongoDB数据库上执行查询:Javascript MongoDB和Mongoose:$where回调中的ES6语法问题,javascript,node.js,mongodb,mongoose,ecmascript-6,Javascript,Node.js,Mongodb,Mongoose,Ecmascript 6,我正在开发Node.JS(6.6.0)应用程序,它使用MongoDB(3.2.10)作为数据库和Mongoose(1.4.X)库。 下面的代码应在MongoDB数据库上执行查询: const JobStat = require('../models/job_stat') const co = require('co') let statService = { getRecentJobsStat () { return co(function* () { // Anoth
const JobStat = require('../models/job_stat')
const co = require('co')
let statService = {
getRecentJobsStat () {
return co(function* () {
// Another async code
let result = yield JobStat
.$where(function () {
let condition = true
// I omit full condition since it does not matter here
return condition
})
.sort({
value: 1
})
.exec()
return result
})
}
}
module.exports = statService
不幸的是,它不起作用并生成此异常:
SyntaxError: missing ; before statement @:2:12
当我将$where回调函数中的变量定义从let更改为var(var condition=true)时,该查询已成功执行。我使用6.6.0版本的Node.js和另一个ES6功能对我来说很好。
所以问题是:这个错误的原因是什么?问题是否存在于我的代码或MongoDB/Mongoose中?谢谢你的帮助
UPD:
我在mongo shell中运行了此查询,得到了完全相同的结果:
传递给
$where
的函数在MongoDB服务器内部运行,因为您使用的是3.2-应该支持该服务器。@JackTuck,我更新了我的问题,您可以在mongo shell中看到它如何在Meinterest中工作。所以我在mongo shell中试一试,它可以复制。但是如果您使用const
而不是让它工作。所以我认为这与ES6无关。难道是mongo大发雷霆,因为在这种情况下,let
是$let
?@JackTuck,我同意你的看法。我在$where
回调函数中尝试了arrow函数,它可以工作,所以这个bug应该只与let
keywork相关。对于那些仍然对此有问题的人,或者搜索这个问题的人,MongoDB不允许“let”声明,但允许“const”,这是最奇怪的。我不得不使用“var”。