如何在mongoose模式中添加子文档

时间:2015-06-15 20:46:42

标签: javascript node.js mongodb mongoose

我正在尝试从node.js / Express创建一个mongoose模式的子文档。

我有两个模式:成员和地址

Member.js

// app/models/member.js

// load mongoose since we need it to define a model
var mongoose = require('mongoose'),
  Schema = mongoose.Schema

var Address = require('./address');

var MemberSchema = Schema({
  FName : String,
  LName : String,
  address : [Address],
  phone : {
    type : String,
    number : String
  },
  email: String,
  gender: String,
  DOB: Date,
  rank : {
    level : String,
    updated: { type: Date, default: Date.now }
  },
  Awards : {
    personal : Boolean,
    award : { type : Schema.Types.ObjectId, ref : 'Award' },
    granted: { type: Date, default: Date.now }
  }
});


module.exports = mongoose.model('Member', MemberSchema);

Address.js

// app/models/address.js

// load mongoose since we need it to define a model
var mongoose = require('mongoose'),
  Schema = mongoose.Schema

var AddressSchema = Schema({
  type : String,
  street1 : String,
  street2 : String,
  City : String,
  State : String,
  Zip : Number,
  Lat : Number,
  Lng : Number
});


module.exports = mongoose.model('Address', AddressSchema);

我的意图是创建一个成员,然后添加一个地址。 成员的POST调用工作。但是,当执行POST调用地址时,它无法说未定义,我找不到错误的位置。我希望了解如何更好地为我的成员架构添加地址。

routes.js

  app.route('/api/member')

    .get(function(req, res) { ...

    .post(function(req, res) {
      var new_member = new member();
      new_member.FName = req.body.fname;
      new_member.LName = req.body.lname;
      new_member.DOB = req.body.DOB;
      new_member.email = req.body.email;
      new_member.gender = req.body.gender;

      new_member.save(function(err) {
          if (err)
              res.send(err);

          res.json({ message: 'Member Created!' });
      });
    });

  app.route('/api/member/:member_id/address')

    .post(function(req, res) {
      member.findById(req.params.member_id, function(err, member) {
        if (err)
          return (err);

        new_address = new address();
        new_address.type = req.body.atype;
        new_address.street1 = req.body.street1;
        new_address.street2 = req.body.street2;
        new_address.City = req.body.City;
        new_address.State = req.body.State;
        new_address.Zip = req.body.Zip;
        new_address.Lat = req.body.Lat;
        new_address.Lng = req.body.Lng;
        console.log(new_address);

        member.address.push(new_address);

        res.json({ message : "Address added!!" });

      });
    });

就像我说的那样,添加会员很容易。添加地址会导致以下错误:

/Users/arcee123/projects/MCARS/node_modules/mongoose/lib/utils.js:419
        throw err;
              ^
TypeError: undefined is not a function
    at Array.MongooseArray._cast (/Users/arcee123/projects/MCARS/node_modules/mongoose/lib/types/array.js:108:30)
    at Object.map (native)
    at Array.MongooseArray.push (/Users/arcee123/projects/MCARS/node_modules/mongoose/lib/types/array.js:262:23)
    at Promise.<anonymous> (/Users/arcee123/projects/MCARS/app/routes.js:108:24)
    at Promise.<anonymous> (/Users/arcee123/projects/MCARS/node_modules/mongoose/node_modules/mpromise/lib/promise.js:162:8)
    at Promise.emit (events.js:107:17)
    at Promise.emit (/Users/arcee123/projects/MCARS/node_modules/mongoose/node_modules/mpromise/lib/promise.js:79:38)
    at Promise.fulfill (/Users/arcee123/projects/MCARS/node_modules/mongoose/node_modules/mpromise/lib/promise.js:92:20)
    at /Users/arcee123/projects/MCARS/node_modules/mongoose/lib/query.js:1833:13
    at model.Document.init (/Users/arcee123/projects/MCARS/node_modules/mongoose/lib/document.js:251:11)
15 Jun 15:45:49 - [nodemon] app crashed - waiting for file changes before starting...

我哪里错了? 感谢

更新1:地址推送的.post调用现在看起来像这样:

.post(function(req, res) {
  member.findById(req.params.member_id, function(err, member) {
    if (err)
      return (err);

    new_address = new address();
    new_address.type = req.body.atype;
    new_address.street1 = req.body.street1;
    new_address.street2 = req.body.street2;
    new_address.City = req.body.City;
    new_address.State = req.body.State;
    new_address.Zip = req.body.Zip;
    new_address.Lat = req.body.Lat;
    new_address.Lng = req.body.Lng;
    console.log(new_address);

    if(member.address === undefined){
        member.address.push(new_address);
    }
    else{
         member.address = [new_address];
    }

    member.save(function(err) {
        if (err)
            res.send(err);

        res.json({ message: 'Address created!!!' });
    });

  });

});

2 个答案:

答案 0 :(得分:2)

由于当您从数据库中检索模型时,Mongoose不需要地址字段,因此不会定义该字段。因此,您将无法添加地址字段。 您应该检查它是否存在

if(member.address === undefined){
    member.address.push(new_address);
}
else{
     member.address = [new_address];
}

编辑:您还必须将其保存回数据库。 (您还可以查看更新功能)

答案 1 :(得分:1)

正如@ Treesrule14所说,如果你没有初始的member.address,你需要将它设置为[new_address]。您将文档保存在第一个路径中,而不是第二个路径。在第二个路径中,您只是在不保存文档的情况下返回文档。

如果您总是想要一个地址字段(即使它是空的),您可以将成员模式中的地址字段更改为address: {type: [address], default:[]}。这将确保默认情况下将设置一个空数组,您可以推送它或读取它的长度,而不必担心它是未定义的。