【问题标题】:User.find() returns empty response in mongodb expressUser.find() 在 mongodb express 中返回空响应
【发布时间】:2020-02-06 10:41:12
【问题描述】:

我正在使用 nextjs 和 express。我正在实现简单的登录表单。我正在发送用户凭据并使用 find() 检查用户是否存在。但是 find() 返回空响应。 在终端 find() 返回该记录的数组。

型号

const mongoose = require('mongoose')
const schema = mongoose.Schema

const user = new schema({
    username: { type: String} ,
    password: { type: String},
    role: { type: String},
})

module.exports = mongoose.model('user', user);

路由器.js

const express = require('express')
const router = express.Router()

const user = require('../models/user');
router.post('/user/signin', (req, res) => {
    user.find({
        username: req.body.username, password: req.body.password
    }, (err, user) => {
        console.log(user);
        if (err) {
            result.status(404).send({ error: 'There is some error' });
        } else if (user.length == 1) {
            var token = 'kkl';//token
            res.send({ token });
        } else {
            console.log(err);
            res.send('Incorrect Email and Password');
        }
    });
})
module.exports = router;

this.is 我的 index.js

const express = require('express')
const next = require('next')
const bodyParser = require('body-parser')
const PORT = process.env.PORT || 4000
const dev = process.env.NODE_DEV !== 'production' //true false
const nextApp = next({ dev })
const handle = nextApp.getRequestHandler() //part of next config
const mongoose = require('mongoose')
const router = express.Router();

nextApp.prepare().then(() => {
    const app = express();
    const db = mongoose.connect('mongodb://localhost:27017/knowledgeBase')
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: true }));
    app.use('/knowledgeBase', require('./routes/router'));
    app.get('*', (req, res) => {
        return handle(req, res) // for all the react stuff
    })
    app.listen(PORT, err => {
        if (err) {
            console.log(err);
            throw err;
        }
        console.log(`ready at http://localhost:${PORT}`)
    })
})

请帮忙

【问题讨论】:

  • 为什么不使用findOne() ??相反。您正在寻找一个用户对吗?。Find 会不必要地遍历所有文档,从而延长您的响应时间
  • 您的代码看起来不错,您确定特定用户名的数据存在吗??可以尝试console.log(req.body.username) 看看用户名是否存在。
  • @Shubh 尝试 findOne() 返回 null
  • 这清楚地表明该用户名的数据在 mongodb 中不存在
  • @Shubh 但是当我在终端尝试它的发送数组时

标签: node.js mongodb express mongoose next.js


【解决方案1】:

当你尝试这个时,你得到了什么回应? 根据回复我将编辑回复。

router.post("/user/signin", async (req, res) => {
  if (!req.body.username) return res.status(400).send("username cannot be null");

  if (!req.body.password) return res.status(400).send("Password  cannot be null");

  const user = await User.findOne({ username: req.body.username});

  if (!user) return res.status(400).send("User not found");

  if (req.body.password!== user.password)
    return res.status(400).send("Invalid password.");

  res.send("logined");
});

【讨论】:

  • 它返回的用户名不能为空
  • @Karthi 抱歉,我在代码中出错了,您可以尝试使用更新后的代码吗?
  • 返回未找到用户
  • @Karthi 这意味着没有具有 req.body.username 的用户。请检查您的收藏是否有此用户名。并且还要确定你是否在控制你的应用使用的数据库。
  • 它就在那里。我尝试使用终端,它返回数组。如何检查我使用的是哪个数据库。
猜你喜欢
  • 2020-08-06
  • 2022-06-29
  • 1970-01-01
  • 2020-01-18
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多