0

I have a method to update the commerce information. There is the possibility to add Workdays to the commerce by sending them in the request body. There following code works fine except that the workdays are not created in mongoDB. They are only saved in the Commerce document (as an array od ids) but the Collection called "workday" is not createdin mongoDB. Why it's not created?

            if(req.body.workdays){
                var workdays = req.body.workdays;
                var lunch = req.body.lunch.split("_");

                commerce.workdays=[];
                for(var i =0, size=workdays.length; i<size; i++ ){
                  var item=new Workday();
                  item.dayOfWeek = workdays[i];
                  item.owner=commerce._id;
                  var range = new Range();
                  range.initial = lunch[0];
                  range.end = lunch[1];
                  range.workday = item;
                  item.ranges.push(range);
                  commerce.workdays.push(item);
                }
              } 

              commerce.save(function(err) {
                if(!err) {
                  log.debug('Updated');
                  res.status(200).send(commerce);
                } else {
                  errorHandler.processError(err, res, log);
                }
              });

here are the models:

var CommerceSchema = new Schema({
  // Common fields.
  createdAt  : {type : Date, default : Date.now},
  location: [Number, Number],
  photos: [{type : Schema.Types.ObjectId, ref : 'Photo'}],
  name: { type: String},
  address: { type: String},
  email: { type: String, default: "-"},
  workdays: [{type : Schema.Types.ObjectId, ref : 'Workday'}],
  description: { type: String, default: "-"},
  phone: { type: Number},
  user: {type : String, ref : 'User'},
  type: [{ type: Number, default: 0}]
});

var WorkdaySchema = new Schema({
  dayOfWeek: { type: Number},
  owner: {type : String},
  ranges: [{type : Schema.Types.ObjectId, ref : 'Range'}],
  createdAt  : {type : Date, default : Date.now}
});

var RangeSchema = new Schema({
  initial: { type: Number},
  end: { type: Number},
  workday: {type : String, ref : 'Workday'}
});

1 Answer 1

2

"workdays" is expecting Mongo ObjectIds. You have to save the individual Workdays first, and then you can add their Ids (_id) to the workdays Array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.