0

My School model has a name, address, etc. and an owner which is a User object.

School.js

module.exports = {
  attributes: {
    name: {
        type: 'string',
        required: true
    },
    address: {
        type: 'string'
    },
    owner: {
      model: 'user'
    },
    ...

When creating a new school I do the following:

create: function(req, res, next) {
    School.create(req.params.all(), function schoolCreated (err, school) {

but what is the easiest way to add the owner (which after login is found in the session at req.session.user)?

I get that I can do

create: function(req, res, next) {
    School.create({'name': req.params.name, 'address': req.params.address, 'owner': req.session.user}, function schoolCreated (err, school) {...

but there must be an easier way. Any examples would help tremendously! Thank you so much!

Chris F.
  • 493
  • 5
  • 17

1 Answers1

1

I'm going to answer the question myself.

It looks like an elegant solution that works for me is to use the Lo-Dash library to merge the request parameters with the user like this:

var school = _.merge(req.params.all(), {'owner': req.session.user});

and pass the newly created school variable to the create method.

School.create(school, function schoolCreated (err, school) {...
Chris F.
  • 493
  • 5
  • 17