Reputation: 2576
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
Upvotes: 1
Views: 2782
Reputation: 203564
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();
});
Upvotes: 4