在另一个用户架构

时间:2018-03-23 13:20:41

标签: node.js mongodb mongoose

遇到以下架构的问题

const FriendRequestSchema = new Schema({
    requester: {
        _id: {
            type: Schema.Types.ObjectId
        }
    },
    profilePicture: String,
    pending: {
        type: Boolean,
        default: true
    }
})

我创建新朋友请求的方式

const friendRequest = new FriendRequest({
    requester: {
        _id: req.user._id
    },
    profilePicture: req.user.profilePicture
})

结果对象

    {
        "pending": true,
        "_id": {
            "$oid": "5ab4fdb07525fd6880d1a6b9"
        },
        "profilePicture": "/assets/profile_icons/glasses.png"
    }

现在的问题是:

  1. 我遗漏了请求者字段

  2. id字段始终不同,并且剂量保存请求者ID

  3. 我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

尝试更改您的架构,如下所示:

 requester: {
            type: Schema.Types.ObjectId,
            required: true,
            ref: 'User', // depends on how you called user schema
        },

用法:

const friendRequest = new FriendRequest({
    requester: req.user._id,
    profilePicture: req.user.profilePicture
})