0

I have been learning about q promises and tried to build up some mock APIs to implement its functionality,While doing so I came across the following error,

Enterprise.forEach is not a function

My API code is as follows,

var mongoose = require('mongoose');

var Enterprise = mongoose.model('Enterprise_gpy');
var q = require('q');

var displayEnterprise = function(req, res) {

  function displayEnterpriseName() {

    var deferred = q.defer();

    Enterprise.forEach(function(err, doc) {

      if (err) {
        console.log('Error Finding Files');
        deferred.reject(err);
      } else {
        var name = Enterprise.enterprise_name;

        deferred.resolve({
          name: name
        });
      }

      return deferred.promise;
    });
  }

  function displayEnterpriseEmail() {


    var deferred = q.defer();

    Enterprise.forEach(function(err, doc) {

      if (err) {
        console.log('Error Finding Files');
        deferred.reject(err);
      } else {
        var email = Enterprise.enterprise_email;

        deferred.resolve({
          email: email
        });
      }

      return deferred.promise;
    });
  }
  q.all([
      displayEnterpriseName(),
      displayEnterpriseEmail()
    ])
    .then(function(success) {
      console.log(500, success);
    })
    .fail(function(err) {
      console.log(200, err);
    });
}
module.exports = {

  displayEnterprise: displayEnterprise
}

2 Answers 2

2

In your code Enterprise is a mongoose schema so when you try to do loop using forEach then got

Enterprise.forEach is not a function

you can use forEach after Enterprise.find(). so use

Enterprise.find({}, function(err, docs) {
   if (err) {
    console.log('Error Finding Files');
    deferred.reject(err);
   } else {
     var names = [];
     docs.forEach (function(doc) {
       var name = doc.enterprise_name;
       names.push(name);// pushed in names array
       //.....
     });
     deferred.resolve({
      names: names
    }); // return all names
   }
});

instead of

Enterprise.find().forEach 

and should use

var name = doc.enterprise_name; instead of var name = Enterprise.enterprise_name;

and

var email = doc.enterprise_email; instead of var email = Enterprise.enterprise_email;

Sign up to request clarification or add additional context in comments.

3 Comments

error persists, TypeError: Enterprise.find(...).forEach is not a function
well it works ,but displays only the first document name and email instead of listing the entire documents in the Enterprise collection
your question was problem in forEach so I shown that. however if you want to return all docs name then pushed in an array and return it. see update answer :) @Idlliofrio
1

forEach only works for arrays, and you're using it on a mongoose model. try this instead:

Enterprise.find().exec(function(err, docs) {
  docs.forEach(function(doc) {
    // do something with all the documents
  }
  // do something outside the loop
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.