chintu
chintu

Reputation: 195

Unable to get documents when I use ES6 in my node js

I'm working on MEAN stack to create some web services. I thought of using ES6 for synchronizing mongodb find operations. Here is the code(UserService):

var Todo = require('../models/user.js');
var db = mongoose.createConnection('mongodb://localhost:27017/abc');
var Users = db.model('User');


function *myGenerator() {
   return yield Todo.find({});//Throwing Undefined function Todo.find
   //return yield Users.find({}); //DOes not returns documents but returns a json object which has full mongodb database details
}

function getDocs(){
   var iterator = myGenerator();
   var firstYield = iterator.next();
}

return yield Todo.find({}) is throwing exception Undefined function Todo.find

return yield Users.find({}); does not return documents but returns a JSON object which has full mongodb database details

return yield Users.find({}).exec() returns following output

{ value:
Promise {
emitter:
EventEmitter {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined },
emitted: {},
ended: false },
done: false }

PS: I used --harmony node js option as well.

Could you please help me to get User rows/documents?

Upvotes: 0

Views: 101

Answers (2)

user8701049
user8701049

Reputation:

Todo.find({}); returns a Query object. You must call exec function on that object to execute the query. e.g.

Todo.find({}).exec(function (error, docs) {
    if (error) {
        // handle
    }

    if (docs) {
        // yeah !!!
    }
})

Also mongoose database connection is asynchronous. So any queries made before the connection is established obviously wont work. Here's a working example..

var mongoose = require('mongoose');
mongoose.Promise = Promise;

var db = mongoose.connect('mongodb://localhost:27017/test', (error) => {

  if (error) {
    throw error
  }

  console.log('DB Connected');

  var Todo = require('./models/user.js');

  var Users = db.model('User');


  function *myGenerator() {
    yield Todo.find({}); // Returns a Query object
    //yield Users.find({}); // Returns a Query object
  }

  function getDocs(){
    var iterator = myGenerator();
    var firstYield = iterator.next();

    // firstYield is a `Query` object

    firstYield.value.exec((error, users) => {
      if (error) {
        throw error;
      }

      console.log(users);
    })
  }

  getDocs();
});

Upvotes: 1

user8701049
user8701049

Reputation:

var Todo = requires('models/user.js'); produces ReferenceError: requires is not defined should be var Todo = require('models/user.js');

maybe even var Todo = require('./models/user.js'); because 'models/user.js' is relative to the node_modules directory

return yield Todo.find({}); should be yield Todo.find({});

As far as I can see this code will throw an Exception. Please provide the actual code and some more info like what version of node.js you are currently running ?

p.S I wrote this in the answers section because I have yet to earned the comment everywhere priveledge

Upvotes: 0

Related Questions