Mongoose填充不填充以及如何为填充字段赋值

时间:2015-11-12 15:14:06

标签: node.js mongodb mongoose

我使用的是Mongodb和Nodejs。

我有两个馆藏项目和用户。

我想根据我将在Project集合中提供的memberId来检索登录集合中的名称。我尝试了下面的代码,其填充为null

我的问题是如何在frontEnd中为memberId中的projectModel赋值。因为它是objectId的类型。我想将值传递给memberId作为" sam @gmail。 COM&#34 ;.基于此,我想从用户架构中检索名称。

Heremy架构:     UserSchema

'use strict';
 var mongoose = require('mongoose'),
 bcrypt = require('bcryptjs'),
 crypto = require('../lib/crypto');
 var userModel = function () {
 var userSchema = mongoose.Schema({
  name: String,
  login: { type: String, unique: true },  //Ensure logins are unique.
  password: String, 
  role: String
  });

 userSchema.pre('save', function (next) {
  var user = this;
 if (!user.isModified('password')) {
   next();
   return;
     }

    next();
   });

  userSchema.methods.passwordMatches = function (plainText) {
    var user = this;
   return bcrypt.compareSync(plainText, user.password);
    };


    return mongoose.model('User', userSchema);
};
   ProjectSchema

    'use strict';

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

   var projectModel = function () {

   var projectSchema =  schema({
    projectName: String,
    projectNo: String,
    startDate: String,
    endDate: String,
    releases:String,
    sprintDuration:String,
    sprintCount:String,
        teamname: String,
        teamno: String,
    memberId :
         {type: schema.Types.ObjectId, ref: 'users'},
    story:            [{
    name: String,
    creator: String,
    date: String,
    desc:String,
    teamMember:String,
    sprintNo: String,
    sprintStartDate: String,
    sprintEndDate: String,
    status: String
    }]
   });
   module.exports = new projectModel();

   router.post('/home', function (req, res) {
    var projectName = req.body.projectName && req.body.projectName.trim();
    var projectNo = req.body.projectNo && req.body.projectNo.trim();
    var memberId = req.body.memberId;
    Project.
        find({})
        .populate('memberId') 
        .exec(function(err, people) {
            if (err) return handleError(err);
            console.log( people);
    });

0 个答案:

没有答案