Friday 9 June 2017

Express.js & Mongoose: access Object attribute name by the request parameter

The Problem

The problem is that I would like to access the attribute of an Object by the variable passed in the request made.

The Route

 public modify(req: express.Request, res: express.Response, next: express.NextFunction)
  {
    Model.findOne({attr: true}, (err, result) => {
      if (err) {
        console.log(err);
      }
      else {
        let copy = result;
        copy.attr.(req.params.var) = false; // here
      }
    });
  }

The Model Structure

export interface Command extends mongoose.Document {
  attr: {
             one:   {value: Boolean},
             two:   {value: Boolean},
             three: {value: Boolean},
             four:  {value: Boolean},
           };

}



via amazingcode12

No comments:

Post a Comment