Tuesday, 23 May 2017

Function parameter overwritten

The following node.js module logs ModelA two times and ModelB two times during initialization, as expected. Then the model parameter is somehow overwritten and when I attempt to use the routes, it always logs ModelA.

module.exports = (model) => {
  console.log(model.modelName);
  return (db) => {
    console.log(model.modelName);

    router.post('/', (req, res, next) => {
      console.log(model.modelName);
      model.insertMany(req.body, (err, docs) => {
        if(err) return next(err);
        res.json(docs);
      });
    });
  };
};

The module is used like this:

const express = require('express');
const router = express.Router();

const plural = require('./plural.js');

module.exports = (db) => {
  router.use('/a', plural(db.ModelA)(db));
  router.use('/b', plural(db.ModelB)(db));

  return router;
};



via raptor

No comments:

Post a Comment