Mongoose .save()没有保存混合类型的新模型,即使调用markModified,也没有.save()上的回调

时间:2015-09-19 00:18:52

标签: node.js mongodb mongoose

我一直在查看之前的问题,并尝试过写markModified,确保我的连接没有失败,使用mongoose.connect并确保我的数据已填充。我的代码如下所示:

应该保存到数据库的机器人的index.js:

var mongoose = require('mongoose');
var Item = require('../app/models/item');
mongoose.connect('mongodb://localhost/mydatabase');
mongoose.connection.on('error', function (err) {
  console.log('mongoose err:', err);
});

/*a lot of stuff in between */

async.parallel(itemCalls, function(err, results) {
              if (err) return console.log(err);
              var collectiveValue = 0;
              for (var i = 0; i < results.length; i++) {
                var result = results[i];
                var item = result.data;
                var itemValue = parseFloat(item.hardData.price.median_price.substring(0, item.hardData.price.median_price.length-1).replace(',', '.'));
                var dbItem = new Item({
                  hardData: {
                    appid: item.hardData.appid,
                    contextid: item.hardData.contextid,
                    assetid: item.hardData.assetid,
                    classid: item.hardData.classid,
                    instanceid: item.hardData.instanceid,
                    amount: item.hardData.amount,
                    missing: item.hardData.missing,
                    price: {
                      lowest_price: item.hardData.price.lowest_price,
                      volume: item.hardData.price.volume,
                      median_price: item.hardData.price.median_price
                    }
                  },
                  softData: {
                    name: item.softData.name,
                    market_name: item.softData.market_name,
                    market_hash_name: item.softData.market_hash_name,
                    icon_url: {
                      normal: item.softData.icon_url.normal,
                      large: item.softData.icon_url.large
                    }
                  }
                });
                dbItem.markModified('hardData.price');
                dbItem.markModified('softData.icon_url');
                dbItem.markModified('hardData');
                dbItem.markModified('softData');
                console.log(dbItem);
                dbItem.save(function(err) {
                  console.log('saved item');
                  if (err) console.log(err);
                });
                offer.items_to_receive[i].value = itemValue;
                collectiveValue += itemValue;
              }
              offer.total_item_value = collectiveValue;
              offers.acceptOffer({tradeOfferId: offer.tradeofferid});
              console.log('Accepted offer ', offer.tradeofferid);
              winston.log('Accepted offer ' + offer.tradeofferid);
            });

以及包含项目模型和架构的item.js:

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

var itemSchema = new Schema({
    hardData: {
    appid: String,
    contextid: String,
    assetid: String,
    classid: String,
    instanceid: String,
    amount: {type: Number, default: 1},
    missing: Boolean,
    price: {
      lowest_price: {type: String, default: '0'},
      volume: {type: String, default: '0'},
      median_price: {type: String, default: '0'}
    }
  },
  softData: {
    name: String,
    market_name: String,
    market_hash_name: String,
    icon_url: {
      normal: {type: String, default: ''},
      large: {type: String, default: ''}
    }
  }
});

var Item = mongoose.model('Item', itemSchema);

module.exports = Item;

保存没有以某种方式执行,因为我没有收到回调,也没有在数据库中获取数据。

1 个答案:

答案 0 :(得分:0)

在将两个脚本的最小版本合二为一之后,我找到了问题的答案,然后才起作用。问题是,item.js文件和index.js有两个独立的mongoose连接。通过将item.js代码更改为以下内容解决了该问题:

module.exports = function(mongoose) {
var Schema = mongoose.Schema;
var itemSchema = new Schema({
  hardData: {
    appid: String,
    contextid: String,
    assetid: String,
    classid: String,
    instanceid: String,
    amount: {type: Number, default: 1},
    missing: Boolean,
    price: {
      lowest_price: {type: String, default: '0'},
      volume: {type: String, default: '0'},
      median_price: {type: String, default: '0'}
    }
  },
  softData: {
    name: String,
    market_name: String,
    market_hash_name: String,
    icon_url: {
      normal: {type: String, default: ''},
      large: {type: String, default: ''}
    }
  }
});
var Item = mongoose.model('Item', itemSchema);
return Item;
}

这意味着可以通过编写var Item = require('/path/to/item')(mongoose)来引用已经存在的mongoose连接。