ReferenceError: 说无法访问“用户”在reactjs中初始化之前

发布于 2025-01-10 18:37:44 字数 2235 浏览 0 评论 0原文

我正在制作一个api,我想将帖子信息存储在Postman的数据库中。我创建了 server.js、userControllers.js、userRoutes 和 userModels.js,但是当我尝试将数据发布到 Postman 时,出现以下错误:

参考错误:无法访问“用户” 初始化之前 在 C:\Users\drago\OneDrive\Documents\Third Year College\Cryptonia\server\controllers\userControllers.js:7:24

我不知道为什么当我去找邮递员时这个错误不会消失,我认为它与 User.findOne(); 有关。在 userController 中,我看到代码已正确编写,之前在发送姓名和电子邮件数据时没有任何问题,现在当我尝试时,我只是在下面收到这些错误,任何帮助都将非常感激如果您知道如何解决此问题,请帮忙,因为这对我有很大好处。

    const asyncHandler = require('express-async-handler');
const User = require('../models/userModel');

const registerUser = asyncHandler(async (req, res) => {
    const { name, email, password, pic } = req.body;

    const userExists = await User.findOne({ email });

    if (userExists) {
        res.status(400)
        throw new Error('User already Exists');
    }

    const user = await User.create({
        name,
        email,
        password,
        pic,
    });


    if (user) {
        res.status(201).json({
            _id: user._id,
            name: user.name,
            email: user.email,
            isAdmin: user.isAdmin,
            pic: user.pic,
        })
    } else {
        res.status(400)
        throw new Error('Error Occured');
    }

    res.json({
        name,
        email,
    });
});

module.exports = { registerUser };

`` //用户模型

const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');

const userSchema = mongoose.Schema(
    {
        name: {
            type: String,
            required: true,
        },
        email: {
            type: String,
            required: true,
            unique: true,
        },
        password: {
            type: String,
            required: true,
        },
        isAdmin: {
            type: Boolean,
            required: true,
            default: false,
        },
        pic: {
            type: String,
            required: true,
            default:
                "https://icon-library.com/images/anonymous-avatar-icon/anonymous-avatar-icon-25.jpg",
        },
    },
    {
        timestamps: true,
    }
);

const User = mongoose.model("User", userSchema);

module.exports = User;

``

I am making an api and i want to store the post information in the database in Postman. I have created a server.js, userControllers.js, userRoutes and userModels.js but when I try and post the data to Postman I get this error below:

ReferenceError: Cannot access 'user'
before initialization
at C:\Users\drago\OneDrive\Documents\Third Year College\Cryptonia\server\controllers\userControllers.js:7:24

I dont know why when I go to postman this error wont go away i think it has something to do with the User.findOne(); in userController I go to this and have seen the code has been written properly and there was no problems before when i was sending the data for name and email now when I try it im just receving these errros down below any help will be much apprecciated and if you know how to resolve this please help as this will be of much benefit to me.

    const asyncHandler = require('express-async-handler');
const User = require('../models/userModel');

const registerUser = asyncHandler(async (req, res) => {
    const { name, email, password, pic } = req.body;

    const userExists = await User.findOne({ email });

    if (userExists) {
        res.status(400)
        throw new Error('User already Exists');
    }

    const user = await User.create({
        name,
        email,
        password,
        pic,
    });


    if (user) {
        res.status(201).json({
            _id: user._id,
            name: user.name,
            email: user.email,
            isAdmin: user.isAdmin,
            pic: user.pic,
        })
    } else {
        res.status(400)
        throw new Error('Error Occured');
    }

    res.json({
        name,
        email,
    });
});

module.exports = { registerUser };

``
//userModel

const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');

const userSchema = mongoose.Schema(
    {
        name: {
            type: String,
            required: true,
        },
        email: {
            type: String,
            required: true,
            unique: true,
        },
        password: {
            type: String,
            required: true,
        },
        isAdmin: {
            type: Boolean,
            required: true,
            default: false,
        },
        pic: {
            type: String,
            required: true,
            default:
                "https://icon-library.com/images/anonymous-avatar-icon/anonymous-avatar-icon-25.jpg",
        },
    },
    {
        timestamps: true,
    }
);

const User = mongoose.model("User", userSchema);

module.exports = User;

``

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文