在尝试为mongodb创建新对象时,req.body.content未定义

时间:2018-03-12 18:06:41

标签: node.js mongodb express mongoose

我正在尝试创建一个简单的API来创建称为实体的不同对象并将它们存储在mongo数据库中。我一直在关注教程here并将其更新以供我自己使用。我也在使用mongoose与数据库进行通信,但是我遇到了一个问题,每当我尝试执行POST请求时,由于req.body.content未定义,它会返回错误。这是我的server.js文件:

var express = require('express');
var bodyParser = require('body-parser');

// create express app
var app = express();

// parse requests of content-type - application/x-www-form-urlencoded
app.use( bodyParser.urlencoded( { extended: true } ) );

// parse requests of content-type - application/json
app.use( bodyParser.json() );

// database requirements
var dbConfig = require( './config/database.config.js' );
var mongoose = require( 'mongoose' );

// Configuring database
mongoose.Promise = global.Promise;

mongoose.connect( dbConfig.url );

mongoose.connection.on( 'error', function()
    {   
        console.log( "Could not connect to the database. Exiting now..." );
        process.exit();
    }   
);

mongoose.connection.once( 'open', function()
    {   
        console.log( "Successfully connected to the database" );
    }   
);

// define a simple route
app.get( '/', function( req, res ) 
    {   
        res.json( { "message" : "Welcome to API. More to come soon...." } );
    }   
);

// Require Entities routes
require( './app/routes/entity.routes.js' )( app );  

// listen for requests
app.listen( 3000, function()
    {   
        console.log( "Server is listening on port 3000" );
    }   
);

这是我的外部routes.js文件:

module.exports = function( app ) { 

   var entities = require( '../controllers/entity.controller.js' );

    // Create a new Entity
    app.post( '/entities', entities.create );

    // Retrieve all Entities
    app.get( '/entities', entities.findAll );

    // Retrieve a single Entity with entityId
    app.get( '/entities/:entityId', entities.findOne );

    // Update an Entity with entityId
    app.put( '/entities/:entityId', entities.update );

    // Delete an Entity with entityId
    app.delete( '/entities/:entityId', entities.delete );

}

我的controller.js文件:

var Entity = require( '../models/entity.model.js' );

exports.create = function( req, res ) 
{

    // create and save a new entity
    console.log( 'req.body: ' + req.body );
    if( !req.body.content ) 
    {   
        console.log( 'req.body.content: ' + req.body.content );

        return res.status( 400 ).send( { message: "Entity can not be empty" } );
    }   

    var entity = new Entity
    ({    
        filename       : req.body.filename, 
        material       : req.body.material,
        design_element : req.body.design_element, 
        style          : req.body.style

    }); 

    entity.save( function( err, data ) 
    {   
        if( err ) 
        {   
            console.log( err );

            res.status( 500 ).send( { message: "Some error occurred while creating the Entity." } );

        }   
        else 
        {   
            res.send( data );
        }   
    }); 

};

还有我的帖子请求的图片。我正在使用Postman来测试我的请求。Post Request Result

我发现一些stackoverflow帖子已经像here那样在定义任何路由之前设置任何app.use()函数,并确保安装了body-parser,但我仍然得到错误。任何帮助将不胜感激。

谢谢!

0 个答案:

没有答案