如何响应参数名称而不是其ID

发布于 2025-02-10 02:15:20 字数 610 浏览 1 评论 0原文

我是后端的初学者, 我已经学习nodejsmongoose

我响应博客文章这样

[
{ 
title:"post title" ,
category : 1,
content : "lorem epsom ... "
},
...
]

,这是我的控制器代码,

exports.get = async (req, res, next) => {
    try {
        const posts= await Post.find()
        res.send(posts)
    } catch (err) { next(err) }
}

但是现在我想要这样回答:

[
{ 
title:"post title" ,
category : "category A",
content : "lorem epsom ... "
},
...
]

将类别保存到名为类别的另一个表中 那么如何发送类别名称而不是其ID?

I am beginner with backend ,
I've learning nodeJs and Mongoose

I respond blog posts like this

[
{ 
title:"post title" ,
category : 1,
content : "lorem epsom ... "
},
...
]

and this is my controller code

exports.get = async (req, res, next) => {
    try {
        const posts= await Post.find()
        res.send(posts)
    } catch (err) { next(err) }
}

But now I want to respond like this :

[
{ 
title:"post title" ,
category : "category A",
content : "lorem epsom ... "
},
...
]

categories are saved into another table named Category
so how can I send category name instead of its Id ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文