[英]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。
如果您想要用於創建客戶端的模塊,請確保您在客戶端目錄中,然后執行以下操作:
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.