我试图通过在控制器中使用此方法来仅呈现一个枚举值:
exports.list = (req, res) => {
let sortBy = req.query.sortBy ? req.query.sortBy : "_id";
Event.find()
.sort([[sortBy]])
.exec((err, events) => {
if (err) {
return res.status(400).json({
error: 'Events not found'
});
}
res.json(events);
});
};
这是我的模型架构:
const eventSchema = new mongoose.Schema({
typeEvent: {
type: String,
default: "Flight",
enum: ["Flight", "Drive"]
},
},
module.exports = mongoose.model('Event', eventSchema);
我想只在React中渲染“ Drive”值的事件。
const getEvents = (sortBy) => {
return fetch(`${API}/events?sortBy=${sortBy}`, {
method: "GET",
})
.then((response) => {
return response.json();
})
.catch((err) => console.log(err));
};
const [showEvents, setShowEvents] = useState([]);
const loadEvents = () => {
getEvents("typeEvent").then((data) => {
if (data.error) {
console.log(data.error);
} else {
setShowEvents(data);
}
});
};
useEffect(() => {
loadEvents();
}, []);
我只能补充一点,这种语法会同时呈现两个值,我错了什么?