首页 > 解决方案 > 修改架构后,猫鼬查询挂起

问题描述

我修改了我的架构,现在只要 mongoose 尝试运行查询,它就会挂起。更糟糕的是,我将架构恢复正常,但它仍然挂起。我需要杀死一些后台进程吗?将代码放在下面,但正如我所说,这以前有效。换了再放回去还是坏了。这是所有查询,而不仅仅是这个。

routes.route('/userInfo/:username')
    .get(async (req, res) => {


        res.send(await userModel.findOne(
            {username:req.params.username}).lean().exec());                
    })

模型设置

var mongoose = require('mongoose'),
    modelName = 'user',
    schemaDefinition = require('../schema/' + modelName),
    schemaInstance = mongoose.Schema(schemaDefinition),
    modelInstance = mongoose.model(modelName, schemaInstance);

module.exports = modelInstance

架构

module.exports = {    
    username: String,
    email: String,
    firstName: String,
    lastName: String,
    createdOn: Date,
    updatedOn: Date,
    scopes: [String]
}

更新:我删除了整个项目并从 github 重新克隆了它。它开始工作了......哇。我之前尝试过删除 node_modules 。我重新启动了我的电脑。这些都没有奏效。为什么废话确实删除了项目工作:(

我不确定这是否重要,但我的模型来自我从 github 存储库中提取的一个 npm 模块(我在几个不同的项目中共享它)。

标签: node.jsmongodbmongoose

解决方案


让我们首先确认一下,确保路线确实被击中,并确保没有错误:

routes.route('/userInfo/:username').get((req, res) => {
  console.log(req.params);
  userModel.findOne({
    username: req.params.username
  }).lean().exec().then(data => {
    console.log(data);
    res.send(data);
  }, err => {
    console.log(err);
    res.status(500).send({ message: 'An error occurred' });
  });
});

我只知道 Express,我不熟悉您在这里使用的任何路由库,但除非其他人另有说明,否则我会假设您做的那部分是正确的。

让我知道这给了你什么。


推荐阅读