我尝试使用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 wordi 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(); });更多推荐
发布评论