![](/img/trans.png)
[英]How can i solve this issue ? TypeError: Cannot read properties of undefined (reading 'map') in react.js
[英]TypeError: Cannot read properties of undefined (reading 'strEmail') How can I solve this problem?
我正在使用 Express.js 編寫此代碼來發出簡單的登錄帖子請求:
app.post("/login", (req, res) => {
res.send(
{
isUserRegistered: userLogin(req.body.strEmail, req.body.strPassword),
}
)
})
function userLogin(strEmail, strPassword) {
if (strEmail.includes("mike@gmail.com") , strPassword.includes("12345")) {
return true;
} else {
return false;
}
}
我的身體(原始):
{
"strEmail":"mike@gmail.com",
"strPassword":"12345"
}
預期的響應是isUserRegistered:True
,這取決於我將在 postman 中傳遞的內容,有什么幫助嗎?
問題出在此處的 scope 上。
創建一個名為utils
的文件夾並在那里創建一個userAuthentication.js
文件。 userAuthentication.js:
function userLogin(strEmail, strPassword) {
if (strEmail === "mike@gmail.com" && strPassword === "12345") {
return true;
} else {
return false;
}
}
module.exports = userLogin;
在您的 app.js 或 index.js 文件中:
const express = require('express');
const userLogin = require('../utils/userAuthentication');
const app = express();
const port = 4000;
app.use(express.json());
app.post('/login', (req, res) =>{
const { strEmail, strPassword } = req.body;
const isAuthenticated = userLogin(strEmail, strPassword);
if (isAuthenticated) {
res.status(200).json({
status: 'Ok',
message: 'A user successfully logged in'
});
} else {
res.status(500).json({
status: 'Fail',
message: 'Wrong credentials'
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.