【问题标题】:Mongoose model is not a constructor猫鼬模型不是构造函数
【发布时间】:2023-01-28 15:38:53
【问题描述】:

用户控制器.js


const uuid = require('uuid')
const bcrypt = require('bcrypt')
const saltRounds = 12;

const User = '../models/user'




async function registerUser(req, res, next) {
    const {displayName, password} = req.body;
    const hash = await bcrypt.hash(password, saltRounds)
    const newUser = new User({
        displayName: displayName,
        password: hash,
        uuid: uuid.v4()
    });

    const response = await newUser.save();

    res.json({
        message: "registration success",
        userCreated: newUser
    })
}

用户.js

const mongoose = require('mongoose');

const userSchema = new mongoose.Schema({
    uuid: {type:String},
    displayName: {type:String},
    password: {type:String}
})

module.exports = mongoose.model('User', userSchema)

错误

TypeError: User is not a constructor

我真的不确定发生了什么,我尝试了不同的导出模型的方法。我查看了其他一些发生此错误的实例,看起来大多数都是由于不正确的导出而发生的。我有另一个我使用的模型,它工作得很好,但是当我在 user-controller.js 中创建它时,它有与用户相同的问题......

【问题讨论】:

    标签: javascript express mongoose mongoose-schema


    【解决方案1】:

    我只是很笨,忘了正确导入模型!

    我做了

    const User = '../models/user'
    

    你需要做

    const User = require('../models/user')
    

    脸谱

    【讨论】:

      猜你喜欢
      • 2017-03-27
      • 2016-04-22
      • 1970-01-01
      • 2019-08-23
      • 2012-01-14
      • 1970-01-01
      • 1970-01-01
      • 2018-01-15
      • 2019-01-18
      相关资源
      最近更新 更多