【问题标题】:NodeJS MySQL API cannot create Users from JSONNodeJS MySQL API 无法从 JSON 创建用户
【发布时间】:2020-08-12 23:00:52
【问题描述】:

首先这是repo网站:https://github.com/TheFJS14/ck-app(可以看到所有相关的代码)

我正在使用 MySQL 开发 NodeJS RESTful API,但是当我尝试发布新的 User json 时,它会报告错误:

ReferenceError: User is not defined
    at exports.create (C:\...\app\controllers\user.controller.js:10:18)

这是我的文件:


exports.create = (req, res) => {
    if (!req.body) {
        res.status(400).send({
            message: "Content can not be empty!"
        });
    }
                 vvvvvvvvv
    const user = new User({
        nameUser: req.body.nameUser,
        emailUser: req.body.emailUser
    });

    User.create(user, (err, data) => {
        if (err)
            res.status(500).send({
                message:
                    err.message || "Some error occurred while creating the user."
            });
        else res.send(data);
    });
};

我还有其他代码参考:

const UserRole = require("../models/userRole.model.js");

exports.create = (req, res) => {
    if (!req.body) {
        res.status(400).send({
            message: "Content can not be empty!"
        });
    }

    const userRole = new UserRole({
        nameUserRole: req.body.nameUserRole,
        descriptionUserRole: req.body.descriptionUserRole
    });

    UserRole.create(userRole, (err, data) => {
        if (err)
            res.status(500).send({
                message:
                    err.message || "Some error occurred while creating the user role."
            });
        else res.send(data);
    });
};

当我将鼠标移到我的用户模型上时,我收到以下消息: https://i.stack.imgur.com/1wjwf.png

但是,当我看到其他代码时,将鼠标悬停在它们上面时看起来会有所不同:https://i.stack.imgur.com/bqsOR.png

我可能需要更改我的变量名,但我不喜欢。 谢谢!

【问题讨论】:

  • 你的10 是什么user.controller.js ??
  • @Panther 我用“v”标记了它

标签: mysql node.js api


【解决方案1】:

我已经在 github 中检查了你的代码,在 user.controller.js 你已经添加了这一行:

const UserRole = require("../models/user.model.js");

你必须把它改成

const User = require("../models/user.model.js");

这就是它抛出 User is not defined

的原因

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-17
    • 1970-01-01
    • 2022-01-14
    • 2018-11-03
    • 2015-10-14
    • 2019-12-16
    • 1970-01-01
    相关资源
    最近更新 更多