所需属性中的Mongoose错误

时间:2017-04-16 00:44:25

标签: node.js mongodb mongoose mongoose-schema

我通过Mongoose将Angular 2应用程序连接到MongoDB。 我正在尝试存储一些数据,但是我在所有必需的属性上都会收到错误。

我设置了一个架构,服务器端:

var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var uniqueValidator = require("mongoose-unique-validator");

var schema = new Schema({
    email: {
        type: String,
        required: true,
        unique: true
    },
    password: {
        type: String,
        required: true
    },
    nome: {
        type: String,
        required: true
    },
    cognome: {
        type: String,
        required: true
    },
    dataNascita: {
        type: Date
    },
    telefono: {
        type: String
    },
    classifica: {
        type: String
    }
});

schema.plugin(uniqueValidator);

module.exports = mongoose.model("User", schema);

用户对象显然已填满:

User Object

Mongoose回应错误:

Error Object

提前感谢您的帮助。 最大

更新

来自Angular服务的电话:

@Injectable()
export class AuthService {

    constructor(private http: Http) {

    }

    addUser(utente: Utente) {
        const body = JSON.stringify(utente);
        return this.http.post('http://localhost:3000/utente', body)
            .map((response: any) => {
                console.log(response);
                response.json();
            })
            .catch((error: Response) => Observable.throw(error.json()
            ));
    }
}

Moongose电话:

var express = require('express');
var router = express.Router();

var User = require('../models/users');

router.post('/', function (req, res, next) {
    var user = new User({
        email: req.body.email,
        password: req.body.password,
        nome: req.body.nome,
        cognome: req.body.cognome,
        dataNascita: req.body.dataNascita,
        telefono: req.body.telefono,
        classifica: req.body.classifica
    });
console.log(res);
    user.save(function (err, result){
        console.log(err);
        console.log(res);
        if (err){
            return res.status(500).json({
                titolo: "Errore durante il salvataggio",
                errore: err
            });
        }
        res.status(201).json({
            messaggio: 'Utente salvato correttamente',
            oggetto: res
        });
    });
});

0 个答案:

没有答案