Javascript 带有wait:Node.js的意外标识符
我应该查询MongoDB,找到名为location的集合的所有元素,并将结果存储在变量中。 我有三个脚本:location.js(在models/location中)、fetcher.js(在fetch/fetcher中)和test.js location.js fetcher.js test.js 调用node test.js时,我收到以下消息:Javascript 带有wait:Node.js的意外标识符,javascript,node.js,mongodb,Javascript,Node.js,Mongodb,我应该查询MongoDB,找到名为location的集合的所有元素,并将结果存储在变量中。 我有三个脚本:location.js(在models/location中)、fetcher.js(在fetch/fetcher中)和test.js location.js fetcher.js test.js 调用node test.js时,我收到以下消息: let items = await fetcher.findAll(); ^^^^^^^ SyntaxEr
let items = await fetcher.findAll();
^^^^^^^
SyntaxError: Unexpected identifier
at createScript (vm.js:74:10)
at Object.runInThisContext (vm.js:116:10)
at Module._compile (module.js:533:28)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Function.Module.runMain (module.js:605:10)
at startup (bootstrap_node.js:158:16)
at bootstrap_node.js:575:3`
如果我删除wait关键字,错误不再出现,但结果是Promise{}
我是javascript和Node.js的新手,不掌握异步调用。你能告诉我哪里错了,怎么解决这个问题吗
注意:我已解决版本节点v8.1.2的问题。只需调用
let items=await fetcher.findAll()
,在一个async函数中调用,就像@ukaszSzewczak建议的那样。因此,我已将代码更新为
async function doSomething(){
let items= await fetcher.findAll()
console.log(items[0].latitude)
// Other code with variable items here ...
}
我下定决心。只需调用
let items=await fetcher.findAll()
,在一个async函数中调用,就像@ukaszSzewczak建议的那样。因此,我已将代码更新为
async function doSomething(){
let items= await fetcher.findAll()
console.log(items[0].latitude)
// Other code with variable items here ...
}
您好@s.dallapalma,您应该在
async
函数中使用await
运算符,await
不能单独使用,至少我没有听说过这个用例@ukaszSzewczak这样回答,因为您是对的:D@ukaszSzewczak await也存在于findAll函数中,那么为什么如果我删除它,结果仍然是错误的呢“承诺{}”“?Hi@s.dallapalma,你应该在async
函数中使用await
操作符,await
不能单独使用,至少我没有听说过这个用例@ukaszSzewczak会这样回答,因为你是对的:D@ukaszSzewczak await也存在于findAll函数中,那么,为什么如果我删除它,结果仍然是“Promise{}”?
let items = await fetcher.findAll();
^^^^^^^
SyntaxError: Unexpected identifier
at createScript (vm.js:74:10)
at Object.runInThisContext (vm.js:116:10)
at Module._compile (module.js:533:28)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Function.Module.runMain (module.js:605:10)
at startup (bootstrap_node.js:158:16)
at bootstrap_node.js:575:3`
async function doSomething(){
let items= await fetcher.findAll()
console.log(items[0].latitude)
// Other code with variable items here ...
}