mongoose markModified数组不工作

时间:2017-12-23 01:25:16

标签: node.js mongodb mongoose

以下代码返回此错误: doc.validate不是函数 。我读过markModified应该解决这个问题......但它没有。这段代码有什么问题?

      StoreRequest.findOne({store: store._id}).populate('store').then(function(storeRequest) {
        [0, 1, 2, 3, 4, 5].forEach(function(i) {
          var newProductUrl = {stuff in here, including nested docs and arrays};
          storeRequest.products[i] = newProductUrl;
          storeRequest.markModified('products');
        })
        storeRequest.save().then(function(sr) {
           return res.json({sr: sr});
        }) 
      }).catch(next)

这是StoreRequestSchema:

StoreRequest.js

var DataSchema = new mongoose.Schema({     ...
});

var ProductRequestSchema = new mongoose.Schema({
  url: String,
  product: { type: mongoose.Schema.Types.ObjectId, ref: 'Product'},
  requests: [{type: mongoose.Schema.Types.ObjectId, ref: 'Request'}],
  deal: { type: mongoose.Schema.Types.ObjectId, ref: 'Deal' },
  data: DataSchema,
});

var ProductMatchSettingsSchema = new mongoose.Schema({
  ...
});

var SiteMapSchema = new mongoose.Schema({
  ...
});

var SpecialRequestSchema = new mongoose.Schema({
  ...
});



var StoreRequestSchema = new mongoose.Schema({
  store: { type: mongoose.Schema.Types.ObjectId, ref: 'Store', unique: true},
  products: [ProductRequestSchema],
  siteMap: SiteMapSchema,
  special: SpecialRequestSchema,
  garbageUrls: [String]  //Urls that have previously been thrown out
});

0 个答案:

没有答案