javascript - 如何使用相关方法在书架中获取计数
问题描述
我正在尝试获得类似的计数值。它返回一个数组我只想知道是否有办法获取数组的计数
router.get('/uploads', (req, res) => {
Image.query((image) => {
image.orderBy('img_url', 'DESC');
image.limit(10);
// if you want to include the user with the image, you would use the withRelated
// comments.user gets the user within the comments array
})
.fetchAll({
withRelated: [
'user',
{
comments: (qb) => {
qb.orderBy('created_at', 'ASC');
},
},
'comments.user',
{
likes: (qb) => {
// how to get count for likes model its returning an array, i just want
// count value
}
}
],
})
.then(images => res.status(200).json(images.toJSON()));
});
解决方案
通过添加 bookshelf-eloquent 来解决它,并添加withCount
现在路由器看起来像这样
router.get('/uploads', (req, res) => {
Image.query((image) => {
image.orderBy('img_url', 'DESC');
image.limit(10);
// if you want to include the user with the image, you would use the withRelated
// comments.user gets the user within the comments array
})
.withCount('likes as likeCount')
.fetchAll({
withRelated: [
'user',
{
comments: (qb) => {
qb.orderBy('created_at', 'ASC');
},
},
'comments.user',
],
})
.then(images => res.status(200).json(images.toJSON()));
});
推荐阅读
- python - 从 Python 中的 h1 剥离 span(美丽的汤)
- javascript - 按钮点击问题
- php - 如何将数组数据从 json 字段显示到刀片视图?
- c# - OTP porgram 出现错误(发送邮件失败)
- javascript - 比较两个对象数组并获取 Javascript 中的差异
- arrays - Vector,VectorX,VectorXi之间的特征差异
- c# - 使用线渲染器制作瞄准轨迹
- python - 为什么将项目附加到 np.array 和 Python 中的列表时方法不同
- ansible - 带有通配符的Ansible复制文件?
- python - 在 web 抓取中使用 python selenium,想要使用 for 循环将 web 表数据打印到控制台