8

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 practicedb 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 = (error.data.error) ? error.data.error : error.data;

   // NOT AUTHENTICATED ERROR
   if(error.status === 401) {
     errorMessage = 'You are not authorized to do this.';
   }

  dispatch({
    type: type,
    payload: errorMessage
  });
}

export function registerUser({ email }) {
  return function(dispatch) {
    axios.post(`${API_URL}/auth/register`, { email })
    .then(response => {
      console.log('THIS IS TESTING PURPOSE')
      console.log(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 = req.body.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.save(function(err, user) {
      if(err) { return next(err); }
      console.log('ERROR 5')
      res.status(201).json({
        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 (practicedb) never get created:

enter image description here

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

EDIT

enter image description here

rishat
  • 7,038
  • 3
  • 35
  • 65
Jo Ko
  • 5,515
  • 12
  • 47
  • 101
  • @Jacob Hey Jacob! Mind taking a look for me. Thanks – Jo Ko Sep 08 '16 at 01:43
  • Is the database running? May you have 2 database instances in different locations? – sailens Sep 09 '16 at 17:34
  • @sailens Registered again and checked my database through terminal, but still shows only `test` database rather than `practicedb` which I defined the new register data to be stored in. And I never created `test` database. Is there a way to see if there are multiple database instances running? – Jo Ko Sep 09 '16 at 18:14
  • On `OPTIONS` preceding `POST`, there's [a good answer for that](http://stackoverflow.com/a/29954326/1287643). – rishat Sep 09 '16 at 18:16
  • BTW, what if you `curl -X POST http://localhost:3000/api/auth/register`? – rishat Sep 09 '16 at 18:17
  • @JoKo Try changing the database url From `'database': 'mongodb://localhost/practicedb',` to `'database': 'mongodb://127.0.0.1:27017/practicedb',` where 27017 is the port at which MongoDB is running. – Dhruv Kumar Jha Sep 09 '16 at 18:20
  • The `db` command in Mongo shell shows the _current_ database (`test`). If you want it to show _all_ databases, use `show dbs` – robertklep Sep 09 '16 at 18:27
  • @robertklep You are the savior! I've been struggling for days and even mentioned my commands to check out dbs, yet no one bothered to point it out... Thank you so much! Please make the answer, and I will accept, upvote it, and hand you the bounty. – Jo Ko Sep 09 '16 at 18:35

1 Answers1

6

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
robertklep
  • 174,329
  • 29
  • 336
  • 330