简体   繁体   English

反应 | 无法发布 | mongodb

[英]REACT | Cannot post | mongodb

I'm working on making a forum and all has been fun until now, at least I thought so... I got toe the register page and made all that BUT, as i send the post request from the page, it causes an error in the server我正在制作一个论坛,到目前为止一切都很有趣,至少我是这么认为的......我进入了注册页面并完成了所有这些但是,当我从页面发送帖子请求时,它会导致错误在服务器中

error:错误:

(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.

But also if I check the.network in the developer tools on google, I see this register 404 xhr content.js:6 420 B 61 ms I knew this isn't supposed to have a 404 err so if I click it, it obviously says Cannot POST /auth/register , but i cant figure out why.但是如果我在谷歌的开发者工具中检查 the.network,我会看到这个register 404 xhr content.js:6 420 B 61 ms我知道这不应该有 404 错误所以如果我点击它,它显然说Cannot POST /auth/register ,但我不知道为什么。 I even get the 404 on every page like the home page.我什至在主页等每个页面上都得到 404。

I tried console logging my my path by doing this:我通过这样做尝试控制台记录我的路径:

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

Here is the output: https://pastebin.com/JnXRzSWM这是 output: https://pastebin.com/JnXRzSWM

At immediate though, I didn't think of this being of any use, but it has proven some sort of web page is trying to be pathed.虽然立即,我不认为这有任何用处,但它已证明某种 web 页面正在尝试被访问。

I checked the client and the server at least over 50 times and I cannot seem to understand the problem but I do know that it has something to do with some sort of misconfiguration with the server.我检查了客户端和服务器至少超过 50 次,我似乎无法理解这个问题,但我知道它与服务器的某种错误配置有关。

Here are the GitHub repos for the root directory and the client directory becuase they wouldn't go together in the same repo.这是根目录和客户端目录的 GitHub 存储库,因为它们不会在同一个存储库中一起使用 go。

root + server client root + 服务器客户端

If you want the modules used to create the client, make sure your in the client directory then do:如果您想要用于创建客户端的模块,请确保您在客户端目录中,然后执行以下操作:

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

Or要么

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

I would really appreciate it if someone could do some testing and looking into it and could tell me what I have to do/fix如果有人可以做一些测试并研究它并可以告诉我我必须做什么/修复什么,我将非常感激

PS.附言。 If you do test it, make sure to add a database key in the.env file in the server directory如果你测试它,确保在服务器目录下的 .env 文件中添加一个数据库密钥

Right, I'm going to just put this out there, I'm an idiot, I didn't read the error correctly and I didn't notice that I made a plain stupid error, 2 Simple Typos.好吧,我要把它放在那里,我是个白痴,我没有正确阅读错误,而且我没有注意到我犯了一个明显的愚蠢错误,2 个简单的错别字。 It is all fixed now.现在都修好了。

FIX: server/models/User.js修复:服务器/模型/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