Keon Keon - 1 year ago 95
Node.js Question

Unable to add data into mongoDB

I'm using Mongoose to create a schema and attempting to add data to mongoDB via Postman and it is adding empty data to the db. I'm assuming my issue is the way the data is parsed from browser to server here is my app.js setup.

what is added to DB

{ "_id" : ObjectId("57846282c7b5f51d4c1742a9"), "__v" : 0 }

Here is the app.js file

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var multer = require('multer');
var routes = require('./routes/index');
var mongoose = require('mongoose');
var db = mongoose.connection;
var Page = require('./models/growling_rabbit_page_text.js');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(bodyParser.urlencoded({ extended: true}));
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);

var storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, './uploads');
filename: function (req, file, callback) {
callback(null, file.fieldname + '-' +;
var upload = multer({ storage : storage}).single('userPhoto');'/api/photo',function(req,res){
upload(req,res,function(err) {
if(err) {
return res.end("Error uploading file.");
res.end("File is uploaded");
var text = new Page(); =; {
if (err)

res.json({ message: 'Text Added!' });


// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;

module.exports = app;

Here is the model

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var pageSchema = new Schema({
homePage: String,
communityPage: String,
eventPage: String,
contactPage: String,
galleryPage: String });

module.exports = mongoose.model('Page', pageSchema);;

Help, I'm sure it is something painfully obvious, unfortunately I cannot find it.

Answer Source

Well you create a Page with

var text = new Page(); =;

but your schema has no name property. Change your schema like so:

var pageSchema = new Schema({
    name: String,
    homePage: String,
    communityPage: String,
    eventPage: String,
    contactPage: String,
    galleryPage: String });

Another problem could be using the wrong option for posting the data via Postman.
Ensure that x-www-form-urlencoded is used when testing your node API


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download