Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

vue+express+mongoose 如何让express获取前端query中的参数

练手的项目,写到搜索的时候卡住了。
是想让vue用$router.query传参,但是不知道怎么传到后端用api搜索。来向大佬们求助。

后端express代码,这里的接口我用postman测试了一下是没有问题的。

    //controller
    find(req, res){
        const keyword=req.query.kw;
        imgsModel.find({name:{ $regex: '.*' + keyword + '.*' } }).sort({ _id:-1})
            .exec((err, imgs) => res.json(imgs))
    },
    
    //express路由
    router.get('/imgs/find',imgsController.find);

postman:
image.png

前端部分我就不会写了,axios.get那里不知道怎么传递query的参数,这样写的话我前端获取的是全部是数据而不是搜索后的数据。

//store
    findImgs({
      commit
    }, payload){
      const { kw } = payload;
      axios.get(`${API_BASE}/imgs?kw=${kw}`).then(response => {
        commit('ALL_IMGS', {
          imgs: response.data
        });
      })
    },

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
`${API_BASE}/imgs?kw=${kw}`

这个地址和postman里面的都不是一个呀。。。

postman 是用的 /imgs/find 接口


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...