繁体   English   中英

反应 | 无法发布 | mongodb

[英]REACT | Cannot post | mongodb

我正在制作一个论坛,到目前为止一切都很有趣,至少我是这么认为的......我进入了注册页面并完成了所有这些但是,当我从页面发送帖子请求时,它会导致错误在服务器中

错误:

(node:15264) UnhandledPromiseRejectionWarning: TypeError: user.issModified is not a function
    at model.<anonymous> (D:\discussa\server\models\User.js:18:15)
    at callMiddlewareFunction (D:\discussa\server\node_modules\kareem\index.js:483:23)
    at model.next (D:\discussa\server\node_modules\kareem\index.js:58:7)
    at _next (D:\discussa\server\node_modules\kareem\index.js:107:10)
    at D:\discussa\server\node_modules\kareem\index.js:508:38
    at processTicksAndRejections (internal/process/task_queues.js:75:11)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:15264) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:15264) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate 
the Node.js process with a non-zero exit code.

但是如果我在谷歌的开发者工具中检查 the.network,我会看到这个register 404 xhr content.js:6 420 B 61 ms我知道这不应该有 404 错误所以如果我点击它,它显然说Cannot POST /auth/register ,但我不知道为什么。 我什至在主页等每个页面上都得到 404。

我通过这样做尝试控制台记录我的路径:

var user = app.use('/api/user', require('./controllers/User'));
console.log(user)

这是 output: https://pastebin.com/JnXRzSWM

虽然立即,我不认为这有任何用处,但它已证明某种 web 页面正在尝试被访问。

我检查了客户端和服务器至少超过 50 次,我似乎无法理解这个问题,但我知道它与服务器的某种错误配置有关。

这是根目录和客户端目录的 GitHub 存储库,因为它们不会在同一个存储库中一起使用 go。

root + 服务器客户端

如果您想要用于创建客户端的模块,请确保您在客户端目录中,然后执行以下操作:

npm install @testing-library/jest-dom @t@testing-library/user-event esting-library/react axios bcryptjs body-parser mongoose nodemon react react-dom react-router-dom react-scripts validator web-vitals

要么

yarn add @testing-library/jest-dom @t@testing-library/user-event esting-library/react axios bcryptjs body-parser mongoose nodemon react react-dom react-router-dom react-scripts validator web-vitals

如果有人可以做一些测试并研究它并可以告诉我我必须做什么/修复什么,我将非常感激

附言。 如果你测试它,确保在服务器目录下的 .env 文件中添加一个数据库密钥

好吧,我要把它放在那里,我是个白痴,我没有正确阅读错误,而且我没有注意到我犯了一个明显的愚蠢错误,2 个简单的错别字。 现在都修好了。

修复:服务器/模型/User.js

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

const UserSchema = new Schema({
    name: String,
    email: String,
    password: String,
    createdAt: {
        defualt: Date.now(),
        type: Date
    },
    role: String,
});

UserSchema.pre( 'save', async function (next) {
    const user = this;
    if (!user.issModified('passwprd')) return next();
              ^^^^^^^^^^^  ^^^^^^^^
// Changed to if (!user.issModified('passwprd')) return next();

    const salt = await bcrypt.genSalt(10)
    const hash = await bcrypt.hash(user.password, salt);
    user.password = hash;
    next()
});

const User = mongoose.model( 'User', UserSchema);
module.exports = User;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM