【问题标题】:mongoose populate not populating the required field猫鼬填充未填充必填字段
【发布时间】:2021-05-06 15:17:35
【问题描述】:

我已经尝试了几天尝试使用生物数据架构值填充用户文档。

我一直只获取用户数据,而不是两种模式的组合。

以下是我一直在使用的代码。

这些是模型:

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

这是猫鼬插件

    const passportLocalMongoose = require('passport-local-mongoose')

用户架构

    const UserSchema = new Schema({
         email: { type: String, unique: true, required: true},
         bios: {
             type: mongoose.Schema.Types.ObjectId,
             ref: 'Bio'
         },
         username: String,
         level: String,
         resetPasswordToken: String,
         resetPasswordExpires: Date
       });

生物数据架构

    const BioSchema = new Schema({
         address: String,
         age: Number,
         gender: String,
         user: {
             type: mongoose.Schema.Types.ObjectId,
             ref: 'User'
         },
       });

使用插件

    UserSchema.plugin(passportLocalMongoose, {usernameField: 'email'})

为模式建模

    const User = mongoose.model('User', UserSchema, 'user');
       const Bio = mongoose.model('Bio', BioSchema, 'bio');
      module.exports =   {User, Bio}

这些是控制器:

    const {User, Bio} = require('../models/users');
    
       module.exports = {  

用户注册回调函数

    signup: async (req, res, next)=>{
                   await User.register(new User(req.body), req.body.password).then(user=>{
                     req.login(user, function(err){
                        if(err) return next(err);
                          })

生物数据回调函数


   postUserDetails: async (req, res, next)=>{
             const newBio = new Bio({
               address: req.body.address,
               age: req.body.age,
               gender: req.body.gender,
             })
             await newBio.save().then(result=>{
               console.log(result)
               res.redirect('/')
             }).catch(err=>{
               console.log(err)})
           },

获取请求填充回调函数


    getUserDetails: async(req, res, next)=>{
          await User.findOne(_req.params._id).populate("bios").then(result=> res.json(result)).catch(err=> console.log(err))
        },
       } 

我没有得到填充字段,bios。我只获取用户数据作为回报。

请有人帮忙。

【问题讨论】:

    标签: node.js express mongoose passport.js


    【解决方案1】:

    试试findOne({}) 和{}

    getUserDetails: async(req, res, next)=>{
          await User.findOne({_req.params._id}).populate("bios").then(result=> res.json(result)).catch(err=> console.log(err))
        },
       } 
    

    或findById() 不带{}

    getUserDetails: async(req, res, next)=>{
          await User.findById(_req.params._id).populate("bios").then(result=> res.json(result)).catch(err=> console.log(err))
    

    【讨论】:

      【解决方案2】:

      您的 user 文档(您在上一个 sn-p 中查询的文档)是否设置了 bios 值,如果有,是否存在具有确切 _id 值的 bio 文档?

      【讨论】:

      • 是的,它确实有一个 'bios' 值。我的问题可能是@rokob,将设置“_id”值。我很困惑如何去做。我想可能会在登录后自动设置。
      猜你喜欢
      • 2015-07-13
      • 2016-10-10
      • 2014-01-08
      • 2015-07-13
      • 2020-01-17
      • 2021-03-22
      • 1970-01-01
      • 2021-05-03
      相关资源
      最近更新 更多