2015-07-12 1 views
0

Это моя схемаMongoose поддокументы Бросив Обязательный Validation

// grab the things we need 
var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 

var UserSchema = require('./user'); 

var inviteeSchema = new Schema({ 
    email: { type: String, required: true, unique: true }, 
    phone: { type: String, required: true, unique: true }, 
}); 

// create a schema 
var sessionSchema = new Schema({ 
    createdby: { type: String, required: true, unique: true }, 
    invitees: [inviteeSchema], 
    created_at: Date, 
    updated_at: Date 
}); 

// on every save, add the date 
sessionSchema.pre('save', function(next) { 
    // get the current date 
    var currentDate = new Date(); 

    // change the updated_at field to current date 
    this.updated_at = currentDate; 

    // if created_at doesn't exist, add to that field 
    if (!this.created_at) 
    this.created_at = currentDate; 

    next(); 
}); 

// the schema is useless so far 
// we need to create a model using it 
var Session = mongoose.model('Session', sessionSchema); 

// make this available to our users in our Node applications 
module.exports = Session; 

И сейчас, я делаю все, как сохранить

router.post('/', function(req, res) { 
    var session = new Session(); 

    //res.send(req.body); 

    session.createdby = req.body.createdby; 
    session.invitees.push({invitees: req.body.invitees}); 

    session.save(function(err) { 
    if(err) res.send(err); 
    res.json({status: 'Success'}); 
    }); 
}); 

Via Почтальон, я пропускание CreatedBy и приглашенный JSON в

[{"email": "1","phone": "1"},{"email": "2","phone": "2"}] 

Но, я всегда получаю требуемую ошибку для телефона и электронной почты.

Я пробовал различные решения из stackoverflow, но ничего не работало. Я также пробовал передать одно значение как {"email": "1","phone": "1"}, но он также вызывает ошибку.

Я даже попытался изменить мою схему, как показано ниже, но я все еще получаю ошибку проверки.

var sessionSchema = new Schema({ 
    createdby: { type: String, required: true, unique: true }, 
    invitees: [{ 
    email: { type: String, required: true, unique: true }, 
    phone: { type: String, required: true, unique: true } 
    }], 
    created_at: Date, 
    updated_at: Date 
}); 

Может ли кто-нибудь помочь мне указать, что я делаю неправильно?

ответ

0

Ну, наконец, после многих попыток, я нашел решение. В моем коде не было ничего плохого. Проблема была в почтальоне.

router.post('/', function(req, res) { 
    var session = new Session(req.body); 

    session.save(function(err) { 
    if(err) res.send(err); 
    res.json({status: 'Success'}); 
    }); 
}); 

Когда я проходил через [{"email": "1","phone": "1"},{"email": "2","phone": "2"}] Почтальон, он получал конвертируется в строку, как у меня был выбор ххх форм-urlencoded. Мне нужно было выбрать raw и application/json, а затем отправить ту же строку, которая отлично работала.

Так что это была проблема в конце тестирования.

Смежные вопросы