繁体   English   中英

ZCCADCDEDB567ABAE643E15DCF0974E503Z 填充用空数组替换 ObjectIds

[英]Mongoose populate replacing ObjectIds with empty array

我一直在尝试使用 mongoose 填充 function 来连接两个模型。 我可以保存一个 object 但是当尝试使用填充检索时,ObjectIds 只是替换为一个空数组。 似乎已经问了很多问题,但没有一个对我有用的解决方案

用户.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Route = require('./route')

var passportLocalMongoose = require('passport-local-mongoose');

const postSchema = new Schema ({
    text: {
        type: String,
        default: '',
        required: true
    }
}, {
    timestamps: true
});


const UserSchema = new Schema({
    firstname: {
        type: String
    },
    posts: [postSchema],
    route: [{
        type: mongoose.Schema.Types.ObjectId, 
        ref: 'Route'
    }]
}, {
    timestamps: true
});

UserSchema.plugin(passportLocalMongoose);

const User = mongoose.model('User', UserSchema);


module.exports = User;

路由.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

const locationSchema = new Schema ({
    id: {
        type: Number,
        default: 0,
        required: true
    },
    address: {
        type: String,
        default: '',
        required: true
    },
    lat: {
        type: Number,
        default: 0,
        required: true
    },
    lng: {
        type: Number,
        default: 0,
        required: true
    }

},{ 
    timestamps: true })

const routeSchema = new Schema ({
    locations: [locationSchema],
    description: {
        journey1: {
            type: String,
            default: '',
            required: false
        },
        journey2: {
            type: String,
            default: '',
            required: false
        },
        journey3: {
            type: String,
            default: '',
            required: false
        },
        journey4: {
            type: String,
            default: '',
            required: false
        }
    }
}, {
    timestamps: true
});

module.exports = mongoose.model('Route', routeSchema);

REST POST 端点内

User.findOne({_id: req.user._id}, function(err,user) {
        if(user) {

            var routed = new Route();
            routed.locations = req.body.locations;
            routed.description = req.body.description;

            user.route.push(routed);
            user.save()
            .then((user) => {
                res.statusCode = 200;
                res.setHeader('Content-Type', 'application/json')
                res.json(user)
            }, (err) => next(err))
        } else {
            console.log("errored")
            err = new Error('User ' + req.body.username + ' not found');
            err.status = 404;
            return next(err);
        }
    })

REST 内 GET 端点

User.findOne({_id: req.user._id})
    .populate('route')
    .then((user) => {
        if(user){
            console.log("user")
            console.log(user)
            console.log("routes")
            console.log(user.route)
            res.statusCode = 200;
            res.setHeader('Content-Type', 'application/json')
            res.json({success: true, routes: user.route});
        }
    }, (err) => next(err))
    .catch((err) => next(err));

如果我删除填充,我会得到类似的东西

[
new ObjectId("61f053af7ba46267f4893f8f")
new ObjectId("61f053af7ba46267f4893f8f")
new ObjectId("61f053af7ba46267f4893f8f")
]

从 GET 端点但将其添加回返回 []。
我的理解是,在“new Route()”中,我正在创建一个新的 Route Object,其 ID 存储在用户模型/文档(?)中。 然后,当我调用 populate mongoose 时,会在 Route 文档中搜索这些 Id 并将它们转换为我想要的对象。 我能想到的唯一问题是我没有正确创建 Route 对象,因此没有 object 与该 Id 一起存储,这就是为什么当我尝试将 Ids 与 Route 对象交换时返回一个空数组的原因。
有什么想法,还是我们都只是在黑暗中跌跌撞撞?

不完全确定这是正确的方法,但我没有像显示的那样实例化 Route object,而是使用 Route.create(...) 方法,然后将其推送到路由数组,现在填充按预期工作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM