Jo Ko Jo Ko - 2 years ago 158
Javascript Question

ReactJS + Redux: Why isn't MongoDB saving data to the database even with correct API requests?

I have a MongoDB/Webpack/NodeJS Express set up in my ReactJS + Redux project.

I am making API calls from action creators in redux, and reach the API server and get a successful status back, yet the data never gets saved and the database never gets created even checking with in terminal

mongo -> dbs
and it doesn't show
database which I named it as.

What could be the issue? Am I missing something?

Any guidance or insight would be greatly appreciated. Thank you

This is my set up for API:

import axios from 'axios';
import { browserHistory } from 'react-router';
import cookie from 'react-cookie';
import { AUTH_USER, AUTH_ERROR } from './types';

const API_URL = 'http://localhost:3000/api';

export function errorHandler(dispatch, error, type) {
let errorMessage = ( ? :;

if(error.status === 401) {
errorMessage = 'You are not authorized to do this.';

type: type,
payload: errorMessage

export function registerUser({ email }) {
return function(dispatch) {`${API_URL}/auth/register`, { email })
.then(response => {
dispatch({ type: AUTH_USER });
.catch((error) => {
errorHandler(dispatch, error.response, AUTH_ERROR)

And my API controller is set up as such:

"use strict";

const User = require('../models/user')

exports.register = function(req, res, next) {
const email =;
console.log('ERROR 1')

if(!email) {
return res.status(422).send({ error: 'You must enter an email address.'})
console.log('ERROR 1')

User.findOne({ email: email }, function(err, existingUser) {
if(err) { return next(err); }
console.log('ERROR 2')
if(existingUser) {
return res.status(422).send({ error: 'That email address is already in use.'})
console.log('ERROR 3')
let user = new User({
email: email,
console.log('ERROR 4'), user) {
if(err) { return next(err); }
console.log('ERROR 5')
user: user,
console.log('ERROR 6')

Configuration for the API:

module.exports = {
'database': 'mongodb://localhost/practicedb',
'port': process.env.PORT || 3000,
'secret': 'dogcat',

The project so far just has an input text field, where it accepts an email address. If the email has already been registered, the API should return the error
That email address is already in use.
and it does.

So I tried console logging to see what the problem is, and the first time I submit the POST request, it logs the following (the terminal showing API console logs):

enter image description here

And if I try to submit the same email again, it throws me the API error that the email is already in use with 422 error, yet the data do not get saved and database (
) never get created:

enter image description here

Also, what is the
request that shows up in terminal? I only made an attempt to
. Lastly, is
why the
log in API server is not logging in chronological order?


enter image description here

Answer Source

You're using the wrong Mongo shell command: db will only show you the current database (test), but if you want to see a list of all databases, you should use show dbs.

If you want to switch databases:

use practicedb

And, if you want to see the collections in the current database:

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