【发布时间】:2020-04-21 15:20:34
【问题描述】:
您好,我希望能够将用户传递到获取请求中,以查看他们发布了哪些项目。
这里是 GET 请求
// @route GET with NAME
// @desc GET All ITEMS WITH NAME
// @access private
router.get('/:user', (req, res) => {
Item.findBy(req.params.user)
.sort({ date: -1})
.then(items => res.json(items))
});
然后我希望能够通过操作文件传递它。
export const getItems = () => dispatch => {
dispatch(setItemsLoading());
axios.get(`/api/items/`).then(res =>
dispatch({
type: GET_ITEMS,
payload: res.data
})
)
.catch(err =>
dispatch(returnErrors(err.response.data, err.response.status))
);
}
如果有人想知道,这里是项目模式
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
//Create Schema
const ItemSchema = new Schema({
name:{
type: String,
required: true
},
user:{
type: String,
required: true
},
date:{
type: Date,
default: Date.now
}
});
module.exports = Item = mongoose.model('item', ItemSchema);
这样的预期结果是
产品列表 “名称”:“项目名称” “日期”:“月/日/年”
我也是 mongoDB/Json 的新手。我来自使用 SQL。
【问题讨论】:
标签: node.js json express mongoose