SyntaxError:节点中的意外标识符(SyntaxError: Unexpected identifier in node)

编程入门 行业动态 更新时间:2024-10-23 19:22:13
SyntaxError:节点中的意外标识符(SyntaxError: Unexpected identifier in node)

我尝试使用nodejs和mongorito orm从mongodb数据库获取mongodb数据库中的数据,但它显示我跟随错误

kaushik@root:~/NodeJS/application$ node run.js /home/kaushik/NodeJS/run.js:16 var posts = yield Users.all(); ^^^^^ SyntaxError: Unexpected identifier at createScript (vm.js:53:10) at Object.runInThisContext (vm.js:95:10) at Module._compile (module.js:543:28) at Object.Module._extensions..js (module.js:580:10) at Module.load (module.js:488:32) at tryModuleLoad (module.js:447:12) at Function.Module._load (module.js:439:3) at Module.runMain (module.js:605:10) at run (bootstrap_node.js:423:7) at startup (bootstrap_node.js:147:9) kaushik@root:~/NodeJS/application$

这是run.js

var Mongorito = require('mongorito') var Model = Mongorito.Model var Users = Model.extend({ collection: 'testing'// collection name }); var posts = yield Users.all(); console.log(posts)

我也尝试'使用严格'但它给出以下错误

SyntaxError: Unexpected strict mode reserved word

i try to fetch data from mongodb database using nodejs and mongorito orm for mongodb database, but its show me following error

kaushik@root:~/NodeJS/application$ node run.js /home/kaushik/NodeJS/run.js:16 var posts = yield Users.all(); ^^^^^ SyntaxError: Unexpected identifier at createScript (vm.js:53:10) at Object.runInThisContext (vm.js:95:10) at Module._compile (module.js:543:28) at Object.Module._extensions..js (module.js:580:10) at Module.load (module.js:488:32) at tryModuleLoad (module.js:447:12) at Function.Module._load (module.js:439:3) at Module.runMain (module.js:605:10) at run (bootstrap_node.js:423:7) at startup (bootstrap_node.js:147:9) kaushik@root:~/NodeJS/application$

here is run.js

var Mongorito = require('mongorito') var Model = Mongorito.Model var Users = Model.extend({ collection: 'testing'// collection name }); var posts = yield Users.all(); console.log(posts)

i also try 'use strict' but its give following error

SyntaxError: Unexpected strict mode reserved word

最满意答案

Mongorito不使用生成器( 不再 ),这意味着你不能使用yield (即便如此,你得到的错误是因为yield只生成器函数中有效,但是你在你的顶层使用它码)。 它现在使用承诺。

如果您愿意使用Node v7(或像Babel这样的转换器 ),您可以使用async/await 。 您的代码看起来像这样:

const Mongorito = require('mongorito'); const Model = Mongorito.Model; const Users = Model.extend({ collection: 'test' }); void async function() { await Mongorito.connect('localhost/test'); let posts = await Users.all(); console.log(posts) await Mongorito.disconnect(); }();

因为await仅适用于async函数,所以上面的代码使用异步IIFE来包装代码。

对于旧版本的Node,您还可以使用常规的promise链:

Mongorito.connect('localhost/test').then(() => { return Users.all(); }).then(posts => { console.log(posts); }).then(() => { return Mongorito.disconnect(); });

Mongorito doesn't use generators (anymore), which means you can't use yield (even so, the error you're getting is because yield is only valid inside generator functions, but you're using it at the top level of your code). It uses promises now.

If you're willing to use Node v7 (or a transpiler like Babel), you can use async/await. Your code would look something like this:

const Mongorito = require('mongorito'); const Model = Mongorito.Model; const Users = Model.extend({ collection: 'test' }); void async function() { await Mongorito.connect('localhost/test'); let posts = await Users.all(); console.log(posts) await Mongorito.disconnect(); }();

Because await only works inside async functions, the above code uses an async IIFE to wrap the code in.

For older versions of Node, you can also use a regular promise chain:

Mongorito.connect('localhost/test').then(() => { return Users.all(); }).then(posts => { console.log(posts); }).then(() => { return Mongorito.disconnect(); });

更多推荐

本文发布于:2023-07-14 20:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1107239.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标识符   节点   意外   SyntaxError   identifier

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!