我正在使用 goolge 登錄構建 MERN 堆疊 CRUD
我在埠:3001 上運行我的服務器,在埠:3000 上運行前端
getAll() {
return axios.get("http://localhost:3001/jobs")
}
嘗試在會話登錄的情況下獲取資料
router.get("/", util.isLoggedin, (req, res) => {
Job.find()
// .populate("author")
.limit(20)
.sort({ jobId: -1 })
.then((jobs) => res.json(jobs))
.catch((err) => res.status(400).json("Error:" err))
})
const util = {}
util.isLoggedin = function (req, res, next) {
console.log(req.isAuthenticated())
if (req.isAuthenticated()) {
next()
} else {
console.log(req.isAuthenticated())
res.redirect("/")
}
}
module.exports = util
我只能在服務器端檢索資料,而不是前端。有什么解決辦法?
源代碼: https ://github.com/jamespark89/mern-communitywebsite
uj5u.com熱心網友回復:
看來你沒有在等待你的承諾..
async getAll() {
return await axios.get("http://localhost:3001/jobs")
}
uj5u.com熱心網友回復:
代替
getAll() {
return axios.get("http://localhost:3001/jobs")
}
和
async getAll() {
return await axios.get("http://localhost:3001/jobs")
}
uj5u.com熱心網友回復:
嘗試將您的 get 請求作為異步函式,我通常這樣做:
router.get("/", util.isLoggedin, async (req, res) => {
try {
const res = await Job.find()
// .populate("author")
.limit(20)
.sort({ jobId: -1 })
res.status(400).json({ res })
} catch(err) {
res.status(400).json("Error:" err)
}
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/508467.html