简体   繁体   English

得到“消息”:“管理员验证失败”,猫鼬,node.js中的错误

[英]Getting “message”:“Admin validation failed” Error in mongoose, node.js

I am getting setHeader error in console, and validation error on RESTclient, dont know what I am doing wrong..help me -- 我在控制台中收到setHeader错误,而在RESTclient上收到验证错误,不知道我在做什么错..帮助我-

Error in console - 控制台错误-

_http_outgoing.js:335
    throw new Error('Can\'t set headers after they are sent.');
          ^
Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:335:11)

Error in RESTClient - RESTClient中的错误-

{"message":"Admin validation failed","name":"ValidationError","errors":{"email":{"properties":{"type":"required","message":"Path `{PATH}` is required.","path":"email"},"message":"Path `email` is required.","name":"ValidatorError","kind":"required","path":"email"}}}

Following is the POST API I am hitting - http://localhost:8000/admin/create 以下是我点击的POST API- http://localhost:8000/admin/create

Request Body - 请求正文-

{
fname: "Test",
lname: "Tester",
email: "techadmin@gmail.com",
password: "12345"
}

---------------MY CODE IS AS FOLLOWS--------- ---------------我的代码如下---------

Following is my server.js file - 以下是我的server.js文件-

var express = require('express'),
    app = express(),
    mongoose = require('mongoose'),
    bodyParser = require('body-parser'),
    port = 8000;
var dbURI = "mongodb://localhost/testdb";

// Connect to the mongo database
mongoose.connect(dbURI);

// Allow app to make POST request
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

var routes = require('./server/routes/routes.js')(app);

app.listen(port, function(){ console.log('App started at port '+ port) });

This is my routes.js file - 这是我的routes.js文件-

var admin = require('../controllers/adminCtrl.js');

module.exports = function(app) {
    // Define all Routes here
    app.post('/admin/create', function(req, res){
        console.log(" In /admin/create Route");
        admin.register(req, res);
    });
}

Following is my adminCtrl.js file - 以下是我的adminCtrl.js文件-

var Admin = require('./../models/admin.js');

var register = function(req, res){ 
    console.log(" In Register Model");
    var fields = {
        fname: req.body.fname,
        lname: req.body.lname,
        email: req.body.email,
        password: req.body.password 
    };

    var admin = new Admin(fields);
    admin.save(function(err, data){
        if(err) {            
            res.send(err);
        }
        res.json({"status":200,"message":"Record inserted successfully", data: data});
    });
};
exports.register = register;

Following is my admin.js(model) file - 以下是我的admin.js(model)文件-

var mongoose = require('mongoose');

var adminSchema = new mongoose.Schema({
    fname: { type: String, require:true },
    lname: { type: String, require:true },
    email : { type: String, required: true, unique: true },
    password: { type: String, require:true },
});

var Admin = mongoose.model('Admin',adminSchema);

module.exports = Admin;

Ok, I fixed the issue on my own. 好的,我自己解决了这个问题。 Problem is - 问题是-

  1. I need to set headers as Content-Type: application/json in REST Client. 我需要在REST Client中将标头设置为Content-Type: application/json
  2. I am sending data where I missed " like fname should be "fname" , so the correct data to be posted is like - 我送数据,我错过了"像FNAME应该是"fname" ,所以要贴正确的数据就像是-

    { "fname": "Test", "lname": "Tester", "email": "techadmin@gmail.com", "password": "12345" }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM