I have a MongoDB connected to Node.js Express, and I have an API controller that accepts POST request and .save() the request body to the database. Yet, even if it console logged the request body correctly and entered inside the .save() method, I checked the terminal with mongo->db
, but still nothing got created.
Is there a way to determine if MongoDB is actually using the MongoDB .save() method? And also how can I check if the MongoDB is properly connected to the server?
Tried express logging as well:
And here is how the server is set up:
const express = require('express'),
app = express(),
logger = require('morgan'),
config = require('./config/main'),
mongoose = require('mongoose'),
bodyParser = require('body-parser'),
router = require('./router');
mongoose.Promise = global.Promise;
mongoose.connect(config.database);
const server = app.listen(config.port);
console.log('Your server is running on port ' + config.port + '.');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(logger('dev'));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "http://localhost:8080");
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization, Access-Control-Allow-Credentials");
res.header("Access-Control-Allow-Credentials", "true");
next();
})
router(app);
And the configuration for the server:
module.exports = {
'database': 'mongodb://localhost/testdatabase',
'port': process.env.PORT || 3000,
'secret': 'secretsecret',
}
EDIT 2
My .save in controller:
user.save(function(err, user) {
if(err) { return next(err); }
res.status(201).json({
user: user
})
})
EDIT 3
Tried logging with mongoose.set('debug', true);
and got the following: