猫鼬/快递人口问题

Posted

技术标签:

【中文标题】猫鼬/快递人口问题【英文标题】:Mongoose/Express population issue 【发布时间】:2021-04-12 04:43:20 【问题描述】:

这是我的架构:

1.记录架构:

const mongoose = require('mongoose')
const RecordsSchema = new mongoose.Schema(
    Title:  type: String, required: true ,
    postedby: [
        type: mongoose.Schema.Types.ObjectId,
        ref: 'user'
    ],
    Author:  type: String, required: true ,
    ISBN:  type: String, required: true ,
    Review:  type: String ,
    SelectedFile:  type: String ,
    Likes:  type: Number, default: 0 ,
    Date:  type: Date, default: Date.now() 
);
module.exports = Records = mongoose.model('record', RecordsSchema, 'record');

这是用户架构:

const mongoose = require('mongoose')
const userSchema = new mongoose.Schema(
    username:  type: String ,
    email:  type: String, required: true, unique: true ,
    records: [
        type: [mongoose.Schema.Types.ObjectId],
        ref: 'record'
    ],
    password:  type: String, required: true ,
    Date:  type: Date, default: Date.now(), immutable: true 
);
module.exports = User = mongoose.model('user', userSchema, 'user');

获取记录的快捷途径:

router.get('/postedby/', (req, res) => 
    Records.findOne()
        .populate('postedby')
        .exec()
        .then(post => 
            if (!post) 
                return res.status(400).json( msg: 'Add Posts' );
            
            else return res.json(post);
        ).catch(err => console.error(err))
);

路线结果:


    "postedby": [],
    "Likes": 0,
    "_id": "5fed8c12a4fb2c1e98ef09f6",
    "Title": "New Age",
    "Author": "Situma Prisco",
    "ISBN": "23422",
    "SelectedFile": "",
    "Review": "",
    "Date": "2020-12-31T08:30:10.321Z",
    "__v": 0
,

我在填充的用户字段(posteddby) 上得到一个空白数组。 请帮忙,我做错了什么?是的,我确实有一个用户登录

【问题讨论】:

使用“Records.findByID()”你得到同样的结果吗? 【参考方案1】:

我实现了你的代码和架构,它对我有用,如果你在数据库中正确存储数据,一切都会好起来的......

注意:

如果postby key的数据没有再次显示,请使用find()而不是findOne(),其他问题需要解决,如猫鼬版本和...,因为您的代码和架构是正确的

find() 对我来说是结果:

[
    
        "postedby": [
            
                "records": [
                    "5ff6bc0bd9e7437184b83f76",
                    "5ff6bc23d9e7437184b83f78",
                    "5ff6bc85f1045a4f102bc0cd",
                    "5ff6bca1f1045a4f102bc0ce",
                    "5ff6bca5f1045a4f102bc0cf",
                    "5ff6bcb3f1045a4f102bc0d0",
                    "5ff6bcc7f1045a4f102bc0d1"
                ],
                "_id": "5ff6b81df463322abc7406ec",
                "Date": "2021-01-07T07:28:15.654Z",
                "email": "a@test.com",
                "password": "$2a$12$wJVDysQxbjuRve.hYn/lbO0rskhwj6y8lwDuEWpCwHeNT/V2mybs.",
                "__v": 7
            
        ],
        "Likes": 1,
        "Date": "2021-01-07T07:47:11.610Z",
        "_id": "5ff6bca1f1045a4f102bc0ce",
        "Title": "farsi",
        "Author": "javid1",
        "ISBN": "1",
        "Review": "1",
        "SelectedFile": "1",
        "__v": 0
    ,
    
        "postedby": [
            
                "records": [
                    "5ff6bc0bd9e7437184b83f76",
                    "5ff6bc23d9e7437184b83f78",
                    "5ff6bc85f1045a4f102bc0cd",
                    "5ff6bca1f1045a4f102bc0ce",
                    "5ff6bca5f1045a4f102bc0cf",
                    "5ff6bcb3f1045a4f102bc0d0",
                    "5ff6bcc7f1045a4f102bc0d1"
                ],
                "_id": "5ff6b81df463322abc7406ec",
                "Date": "2021-01-07T07:28:15.654Z",
                "email": "a@test.com",
                "password": "$2a$12$wJVDysQxbjuRve.hYn/lbO0rskhwj6y8lwDuEWpCwHeNT/V2mybs.",
                "__v": 7
            
        ],
        "Likes": 1,
        "Date": "2021-01-07T07:47:11.610Z",
        "_id": "5ff6bca5f1045a4f102bc0cf",
        "Title": "farsi",
        "Author": "javid1",
        "ISBN": "1",
        "Review": "1",
        "SelectedFile": "1",
        "__v": 0
    ,
    
        "postedby": [
            
                "records": [
                    "5ff6bc0bd9e7437184b83f76",
                    "5ff6bc23d9e7437184b83f78",
                    "5ff6bc85f1045a4f102bc0cd",
                    "5ff6bca1f1045a4f102bc0ce",
                    "5ff6bca5f1045a4f102bc0cf",
                    "5ff6bcb3f1045a4f102bc0d0",
                    "5ff6bcc7f1045a4f102bc0d1"
                ],
                "_id": "5ff6b81df463322abc7406ec",
                "Date": "2021-01-07T07:28:15.654Z",
                "email": "a@test.com",
                "password": "$2a$12$wJVDysQxbjuRve.hYn/lbO0rskhwj6y8lwDuEWpCwHeNT/V2mybs.",
                "__v": 7
            
        ],
        "Likes": 1,
        "Date": "2021-01-07T07:47:11.610Z",
        "_id": "5ff6bcb3f1045a4f102bc0d0",
        "Title": "riyazi",
        "Author": "javid1",
        "ISBN": "1",
        "Review": "1",
        "SelectedFile": "1",
        "__v": 0
    ,
    
        "postedby": [
            
                "records": [
                    "5ff6bc0bd9e7437184b83f76",
                    "5ff6bc23d9e7437184b83f78",
                    "5ff6bc85f1045a4f102bc0cd",
                    "5ff6bca1f1045a4f102bc0ce",
                    "5ff6bca5f1045a4f102bc0cf",
                    "5ff6bcb3f1045a4f102bc0d0",
                    "5ff6bcc7f1045a4f102bc0d1"
                ],
                "_id": "5ff6b81df463322abc7406ec",
                "Date": "2021-01-07T07:28:15.654Z",
                "email": "a@test.com",
                "password": "$2a$12$wJVDysQxbjuRve.hYn/lbO0rskhwj6y8lwDuEWpCwHeNT/V2mybs.",
                "__v": 7
            
        ],
        "Likes": 1,
        "Date": "2021-01-07T07:47:11.610Z",
        "_id": "5ff6bcc7f1045a4f102bc0d1",
        "Title": "zaban",
        "Author": "javid1",
        "ISBN": "1",
        "Review": "1",
        "SelectedFile": "1",
        "__v": 0
    ,
    
        "postedby": [
            
                "records": [
                    "5ff6c275964d062f045e93d3",
                    "5ff6c283964d062f045e93d5"
                ],
                "_id": "5ff6c253964d062f045e93d2",
                "Date": "2021-01-07T08:01:21.499Z",
                "email": "b@test.com",
                "password": "$2a$12$jmHUrTSPwjaVd0VEIpsGauExHNSukHRyWWiJt4UlEgeWLBo8GPDH.",
                "__v": 2
            
        ],
        "Likes": 1,
        "Date": "2021-01-07T08:01:21.990Z",
        "_id": "5ff6c275964d062f045e93d3",
        "Title": "zaban",
        "Author": "javid1",
        "ISBN": "1",
        "Review": "1",
        "SelectedFile": "1",
        "__v": 0
    ,
    
        "postedby": [
            
                "records": [
                    "5ff6c275964d062f045e93d3",
                    "5ff6c283964d062f045e93d5"
                ],
                "_id": "5ff6c253964d062f045e93d2",
                "Date": "2021-01-07T08:01:21.499Z",
                "email": "b@test.com",
                "password": "$2a$12$jmHUrTSPwjaVd0VEIpsGauExHNSukHRyWWiJt4UlEgeWLBo8GPDH.",
                "__v": 2
            
        ],
        "Likes": 1,
        "Date": "2021-01-07T08:01:21.990Z",
        "_id": "5ff6c283964d062f045e93d5",
        "Title": "tttt",
        "Author": "javid1",
        "ISBN": "1",
        "Review": "1",
        "SelectedFile": "1",
        "__v": 0
    
]

【讨论】:

它不会改变结果。 我实现了你的代码和模式,对我有用,数据是否正确存储在数据库中? Mohammad,非常感谢您抽出宝贵时间对此做出回应。但是,我仍然得到空白数组....还有什么我可能不看的? 我认为您必须重置数据库,删除所有集合并再次存储数据后,由猫鼬的 populate() 管理的关系将起作用。

以上是关于猫鼬/快递人口问题的主要内容,如果未能解决你的问题,请参考以下文章

使用KeystoneJs的猫鼬两级人口[重复]

猫鼬填充返回空数组

仅获取猫鼬虚拟填充的长度

如何在猫鼬中异步填充嵌套对象?

Mongoose:深人口(填充人口密集的领域)

Mongoose:深人口(填充人口密集的领域)