jeudi 30 juillet 2015

How to store checkbox values in a field of type Array in mongo

I am creating checkboxes with a loop in angular:

<span ng-repeat="role in roles">
      <label class="checkbox" for="{{$index}}">
        <input type="checkbox" ng-model="formData.selected.ids[$index]" ng-value="role" ng-required="!formData.selected" id="{{$index}}" />
        {{sport}}
      </label>
    </span>

schema looks like this :

var UserSchema = new Schema({
  name: String,
  email: { type: String, lowercase: true },
  role: [{
    type: String,

    default: 'user'
  }],
  hashedPassword: String,
  provider: String,
  salt: String
});

At backend, we call:

exports.create = function (req, res, next) {
  var newUser = new User(req.body);
  newUser.provider = 'local';
  console.log(req.body);
  newUser.role = req.body.role;
  newUser.save(function(err, user) {
    if (err) return validationError(res, err);
    var token = jwt.sign({_id: user._id }, config.secrets.session, { expiresInMinutes: 60*5 });
    res.json({ token: token });
  });
};

How do we save checkbox values in the role array ?




Aucun commentaire:

Enregistrer un commentaire