通过 React 中的路由在数据库中查找与用户链接的数组?

Find arrays in db linked with a user through routes in React?

我有一条路线正在从我的 json 数据库中收集数组。

我只想获取 user_id 匹配的数组。

这是我的代码:

router.route("/").get((req, res) => {
Bet.find()
    .then(bets => res.json(bets))
    .catch(err => res.status(400).json("Error: " + err));
});

使用此代码,我将检索 json 中的所有结果。

我试过 Bet.find({ user_id: req.body.user_id }) 但 return 没有任何效果。 user_id 是 json 中的对象。

这是我想要的东西,但这段代码对我来说效果不佳: Github - mern plaid

您应该使用 findByID 方法,看看这是否解决了问题。

router.route('/').get(function (req, res) {

let id = req.body.user_id ;
Bet.findById(id, function (err, bets ) {
    res.json(bets );
});

})

解决了这个问题:

router.route("/:id").get((req, res) => {
Bet.find({ user_id: req.params.id })
    .then(bets => res.json(bets))
    .catch(err => res.status(400).json("Error: " + err));
});