2016-09-07 87 views
8

我在我的ReactJS + Redux項目中設置了一個MongoDB/Webpack/NodeJS Express。ReactJS + Redux:即使有正確的API請求,爲什麼MongoDB不會將數據保存到數據庫?

我正在從Redux的行動創造者的API調用,並達到API服務器並獲得成功的狀態回來了,但數據永遠不會被保存,並且數據庫永遠不會被創建即使在終端mongo -> dbs檢查,並沒有顯示practicedb我將其命名爲的數據庫。

可能是什麼問題?我錯過了什麼嗎?

任何指導或見解將不勝感激。謝謝

這是我設置了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:

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

到目前爲止,該項目只有一個輸入文本字段,它接受一個電子郵件地址。如果電子郵件已經註冊,那麼API應該返回錯誤That email address is already in use.並且它會。

於是,我控制檯日誌記錄,看看是什麼問題,和我第一次提交POST請求時,它記錄以下(表明API控制檯日誌終端):

enter image description here

如果我嘗試再次提交相同的電子郵件,它拋出我的API錯誤的電子郵件已在使用中有422錯誤,但該數據沒有得到保存和數據庫(practicedb)永遠不會創建:

enter image description here

另外,在終端上顯示的OPTIONS請求是什麼?我只是試圖POST。最後,是OPTIONS爲什麼登錄API服務器的ERROR沒有按時間順序登錄?

編輯

enter image description here

+0

@Jacob嘿雅各!介意找我。謝謝 –

+0

數據庫在運行嗎?你可能在不同的位置有2個數據庫實例嗎? – sailens

+0

@sailens再次註冊並通過終端檢查我的數據庫,但仍然只顯示'test'數據庫而不是'practicedb',我定義了要存儲的新寄存器數據。而且我從未創建'test'數據庫。有沒有辦法查看是否有多個數據庫實例在運行? –

回答

6

您使用了錯誤的蒙戈shell命令:db將只顯示你當前的數據庫(test),但如果你想看到的所有數據庫的列表,你應該使用show dbs

如果要切換數據庫:

use practicedb 

而且,如果你想看到在當前數據庫中的集合:

show collections 
+0

謝謝羅伯特!它說我可以在22個小時內獎勵我的賞金。我會解決它! –

相關問題