Aurelia
Aurelia

Reputation: 1062

mongoose & lodash: __.map returns array of nulls

We have a many-to-many partnership relationship in Mongoose. The object is suppoosed to iterate over all objects it references before saving. We're trying to use lodash to generate functions with the right context, but all we get is an array of null

Here's the code:

PartnerSchema.pre("save", function(done) {
  var updaters = __.map(this.partner, function(partner) { // this.partner is an array of ObjectIds
    // this runs, and the argument is fine
    var update = function updateThing(cb) {
      Company.findOne({_id: partner}, function(err, company) {
        if (err) return cb(err);
        company.partners = __.map(company.partners, function(partner) {
          if (partner._id === this._id) {
            return this;
          }
          else {
            return partner;
          }
        })
        company.save(function(err, rowsAffected) {
          cb(err);
        })
      })
    }
    console.log(update); // [Function]
    return update;
  })
  // updaters is now [null, null]
  async.parrallel(updaters, function() {
    done();
  })
})

EDIT: we found out why no error was emitted. Mongoose ate them all!. Downgrade to 4.0.8 shows - at least - the error now. Undefined is not a function, just as expected.

Upvotes: 0

Views: 399

Answers (1)

vmkcom
vmkcom

Reputation: 1668

var updaters = _.map([1,2,3], function(i) {
  var update = function updateThing(j) { return console.log(i, j) };
    console.log(update);
    return update
})

that sample code working fine on lodash website

Try to check your this.partner argument and check that you not redefine your update and updaters variables anywhere in that module

If you had problems - just go from that sample code in new file and extend it so that you can find what's wrong

Upvotes: 1

Related Questions