猫鼬/快递人口问题

问题描述

这是我的架构:

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,ISBN: { type: String,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,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) 上得到一个空白数组。 请帮忙,我做错了什么?是的,我确实有一个用户登录

解决方法

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

注意:

如果postedby 密钥没有再次显示数据,请使用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","_id": "5ff6bca5f1045a4f102bc0cf","_id": "5ff6bcb3f1045a4f102bc0d0","Title": "riyazi","_id": "5ff6bcc7f1045a4f102bc0d1","Title": "zaban",{
        "postedby": [
            {
                "records": [
                    "5ff6c275964d062f045e93d3","5ff6c283964d062f045e93d5"
                ],"_id": "5ff6c253964d062f045e93d2","Date": "2021-01-07T08:01:21.499Z","email": "b@test.com","password": "$2a$12$jmHUrTSPwjaVd0VEIpsGauExHNSukHRyWWiJt4UlEgeWLBo8GPDH.","__v": 2
            }
        ],"Date": "2021-01-07T08:01:21.990Z","_id": "5ff6c275964d062f045e93d3","_id": "5ff6c283964d062f045e93d5","Title": "tttt","__v": 0
    }
]

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...