我正在尝试制作博客应用。当我收到所有帖子时,我希望帖子内容被截断为140个字符以进行预览。
我尝试使用$ substr聚合,但是找不到如何在猫鼬中正确使用它们的方法。
// @route GET api/posts
// @desc Get all users posts
// @access Public
router.get('/',async (req,res) => {
try {
const Posts = await Post.find()
.populate('user','name')
.sort({
date: -1
});
res.json(Posts);
} catch (err) {
console.error(err.message);
res.status(500).send('Server Error');
}
});
我希望输出返回posts数组,其中每个posts内容都被截断为140个字符。