【发布时间】:2022-01-13 13:17:46
【问题描述】:
我想了解如何显示用户的用户名。例如用户 admin 发布了一个论坛,然后我会在论坛页面上看到 Created By: admin,而我只能找出 ID。
我对猫鼬了解不多,我需要熟悉它的人。
我的论坛模型:
你看我只有 ref: 'user' 并且这是从用户那里获取 ObjectId("")。
const forumSchema = ({
forumName: {
type: String,
required: true,
},
forumDescription: {
type: String,
required: true,
},
user: {
type: Schema.Types.ObjectId,
ref: 'user'
},
published_on: {
type: String,
default: moment().format("LLL")
},
});
我的用户模型:
const UserSchema = mongoose.Schema({
userID: {
type: String,
required: true,
},
userName: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
isAdministrator: {
type: Boolean,
deafult: false,
},
});
前端:
正如你只能在 {forum.user} 看到的那样,我可以看到用户的 id,但我想要他的名字而不是 id
<footer className="blockquote-footer">
Created by:{forum.user}
Created on:{forum.published_on.substring(0,300)}
</footer>
【问题讨论】:
-
能否把负责返回论坛对象的方法发到后台?
-
@LucaPizzini 我编辑了它
标签: javascript node.js reactjs mongodb mongoose