ReactJS + Redux:即使有正确的API请求,为什么MongoDB不把数据保存到数据库?

我的ReactJS + Redux项目中有一个MongoDB / Webpack / NodeJS Express。

我正在使用redux中的动作创build者进行API调用,然后到达API服务器并返回成功状态,但是数据永远不会被保存,甚至在terminalmongo -> dbs也不会创build数据库,并且不会显示我把它命名为。

可能是什么问题? 我错过了什么吗?

任何指导或见解将不胜感激。 谢谢

这是我的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) }); } } 

而我的API控制器是这样设置的:

 "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') } 

API的configuration:

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

到目前为止,该项目只有一个input文本字段,它接受一个电子邮件地址。 如果电子邮件已经被注册,则API应该返回错误That email address is already in use. 它确实如此。

所以我尝试了控制台日志logging来查看问题所在,并且在我第一次提交POST请求时,它会logging以下内容(显示API控制台日志的terminal):

在这里输入图像描述

如果我尝试再次提交相同的电子邮件,它会抛出电子邮件已在使用422错误的API错误,但数据不会被保存,数据库( practicedb )永远不会被创build:

在这里输入图像描述

另外,什么是OPTIONS请求显示在terminal? 我只是尝试了POST 。 最后, OPTIONS是为什么API服务器中的ERROR日志不按时间顺序login?

编辑

在这里输入图像描述

您正在使用错误的Mongo shell命令: db将只显示当前数据库( test ),但如果您想查看所有数据库的列表,则应使用show dbs

如果你想切换数据库:

 use practicedb 

而且,如果您想查看当前数据库中的集合:

 show collections