2017-08-09 65 views
-1

我在反應和節點上做了usewr註冊登錄。我的註冊路線正在工作,用戶保存在mongo中,但是登錄路線不起作用?用戶註冊使用Mongo和NodeJs不能正常工作

簽到組件: -

signIn(){ 

axios.post('/tasks/signin', { 
    email: this.state.email, 
    password: this.state.password, 
    }) 
    .then(function (response) { 
    console.log(response); 
    }) 
    .catch(function (error) { 
    console.log(error); 
    }); 
} 

航線有: -

Router.post('/signin', (req, res) => { 
var User = new db1(); 

    User.findOne({ email: req.body.email , password: req.body.password 
}, function(err, user) { 
    console.log(user); 
    if(err) return next(err); 
    if(!user) return res.send('Not logged in!'); 
    return res.send('Logged In!'); 
    }); 
}); 

錯誤:

User.findOne is not a functionand i am getting 500 status.

請幫我在哪裏錯了。

+0

什麼是db1?您是使用Mongoose還是其他框架連接到Mongo? –

+0

@MarkS。 Db1是model const db1 = require('../ models/users'); –

回答

1

findOne方法在模型上,而不是對象。所以它應該是:

db1.findOne({ email: req.body.email , password: req.body.password 

See previous question

1

在我看來,你需要模型並將其分配給一個變量調用db1並創建一個名爲User的實例。之後,你調用實例的findOne方法而不是模型本身。

如果您使用貓鼬,這應該是問題。如果沒有,請提供一些更多細節。

另外,通常使用PascalCase和您的實例與camelCase一起調用您的類是一個好習慣。這樣你就不會感到困惑。

+0

是的,你是對的我使用moongoose和db1是可變的。我現在該做什麼? –

+0

嘗試調用db1的findOne方法,而不是在用戶上。 –