These are chat archives for Automattic/mongoose

12th
Feb 2018
pixiuPL
@pixiuPL
Feb 12 2018 09:26
@markstos this ammount of unclosed connections clearly indicates an attack. Block ALL incoming access on firewall for, say, 1 minute and see what happens
@markstos and also use Wireshark to dive deep
Chris Rutherford
@cjrutherford
Feb 12 2018 15:20

hey guys, how would one use a model schema as a child schema with mongoose? I was thinking about doing it something like this:

const mongoose = require('mongoose');
const Contact = require('./contact');
const Schema = mongoose.Schema;

var InsiderSchema = new Schema({
  firstName: {
    type: String,
    required: true
  },
  preferredName:{
    type: String
  },
language: {
    type: String,
    enum: ['ES','EN','FR','ES-FR','EN-ES','EN-FR'],
    default: 'EN',
    required: true
  },
  contacts: [
    {
      type: Contact
    }
  ]
});

would that work?

Kev
@lineus
Feb 12 2018 15:28
does require('./contact') export a schema or a model as implied by the capital C?
Chris Rutherford
@cjrutherford
Feb 12 2018 15:32
yes it does
Chris Rutherford
@cjrutherford
Feb 12 2018 15:42
actually I found it! it's going to follow the subdocument pattern:
var childSchema = new Schema({ name: 'string' });

var parentSchema = new Schema({
  // Array of subdocuments
  children: [childSchema],
  // Single nested subdocuments. Caveat: single nested subdocs only work
  // in mongoose >= 4.2.0
  child: childSchema
});
Chris Rutherford
@cjrutherford
Feb 12 2018 20:37
having some trouble with my CRUD routes.... I'm working on the Create route, and I'm getting an error stating that my model "Isn't a constructor"....
const mongoose = require('mongoose');
const Area = require('../models/area');
  create(req, res) {
    let _area = req.body;
    let area = new Area(_area);
    area.save((err, area) => {
      if(err){
        log.error(err);
        res.status(500).send('Database Error Saving Area');
      }else {
        res.status(200).send(area);
      }
    });
  }
Chris Rutherford
@cjrutherford
Feb 12 2018 20:45
I think it's something to do with the fact I never converted my schemas to models....
Kev
@lineus
Feb 12 2018 20:54
what's your export line look like in ../models/area ?
@cjrutherford
Chris Rutherford
@cjrutherford
Feb 12 2018 20:56
thanks again Kev, I looked it up and from what I understand in the docs is that you're supposed to var Model = mongoose.model('model name', ModelSchema); then module.exports = Model' which I wasn't doing.....
now I'm having trouble with a subdocument, but it's going to have to wait til tomorrow.