我正在嘗試從資料庫中為某個創建者或用戶獲取資料,但即使創建者不存在,它也會顯示資料。這是代碼
app.use('/api/places', placesRoutes);
router.get('/user/:uid', placesControllers.getPlacesByUserId);
const getPlacesByUserId = async(req, res, next) => {
console.log("request data!", req.params.uid);
const userId = req.params.uid;
let places;
try {
places = await Place.find({ creater: userId });
} catch (err) {
const error = new HttpError('Something went wrong, could not find a place for the given user.',500);
return next(error);
}
if(!places || places.length === 0){
return next(new HttpError('Could not find a place for the provided userid.',404));
}
res.json({ places: places.map( place => place.toObject({ getters: true }) ) });
};
這是保存在 mondo db 中的資料條目
{"_id":{"$oid":"62ab10baa6f33b1c588dfb8e"},"title":"ifel tower","description":"big tower","image":"https://pixabay.com/images/search/nature/","address":"Circular Rd, Walled City of Lahore, Lahore, Punjab 54000, Pakistan","location":{"lat":{"$numberDouble":"31.5924979"},"lng":{"$numberDouble":"74.3073198"}},"creator":"u1","__v":{"$numberInt":"0"}}
它應該只在這個 url 上顯示資料
api/地方/用戶/u1
但它在
具有不同 url 的不同創建者 ID 的資料上顯示相同的資料
uj5u.com熱心網友回復:
我認為這與以下行中的錯字有關:
places = await Place.find({ creater: userId });
我猜創作者應該是創作者。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/494974.html
上一篇:即使在發送回應后仍執行中間件
