简体   繁体   中英

How to push data to a mongoose array in collection?

I'm creating a CronJob to update every key from my currencies collection every 00:01. This is my currencies colection schema:

const CurrencySchema = new mongoose.Schema({
    currency: {
        type: String,
        unique: true,
        required: true
    },
    name: {
        type: String,
        required: true
    },
    exchange_rate: {
        type: SchemaTypes.Double,
        required: true
    },
    spread: {
        type: SchemaTypes.Double,
        required: true,
        default: 0,
        select: false
    },
    lastUpdate: {
        type: Date
    },
    createdAt: {
        type: Date,
        default: Date.now
    },
    history: [{
        date: Date,
        rate: SchemaTypes.Double
    }]
});

I tried to create a CronJob like this:

const CronJob = require("cron").CronJob;
const currencies = require("../../models/currencies");

module.exports = new CronJob("* 1 0 * * *", async function() {
    await currencies.find({}).map(currency => currency.history.push({
        date: Date.now(),
        rate: currency.exchange_rate
        })
    )
});

So, for every currency , I want to push to the history array the Date.now() and the exchange_rate of the currency at that moment.

The problem is that I get this exception:

UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'push' of undefined

How should I fix it?

Thanks!

Edit: a console.log(currency) inside the map function shows this:

[
  {
    _id: 5cbdefd4f5bcec257fcec791,
    currency: 'VES',
    exchange_rate: 5321.709437805213,
    createdAt: 2019-04-22T16:46:12.350Z,
    __v: 0,
    lastUpdate: 2019-05-15T20:40:08.649Z,
    name: 'Venezuelan Bolivar',
    history: []
  },
  {
    _id: 5cbdf078f5bcec257fcec792,
    currency: 'BTC',
    exchange_rate: 7290,
    createdAt: 2019-04-22T16:48:56.182Z,
    __v: 0,
    lastUpdate: 2019-05-15T20:41:01.122Z,
    history: []
  },
  {
    _id: 5cbe1ebccd6e6a2b4738070d,
    currency: 'BRL',
    exchange_rate: 4.2382007085048015,
    createdAt: 2019-04-22T20:06:20.796Z,
    __v: 0,
    lastUpdate: 2019-05-15T20:41:02.817Z,
    name: 'Brazilian Real',
    history: []
  },
  {
    _id: 5cbe1ec8cd6e6a2b4738070e,
    currency: 'INR',
    exchange_rate: 78.43526089163238,
    createdAt: 2019-04-22T20:06:32.322Z,
    __v: 0,
    lastUpdate: 2019-05-15T20:41:02.814Z,
    name: 'Indian Rupee',
    history: []
  },
  {
    _id: 5cbe1ecfcd6e6a2b4738070f,
    currency: 'ZAR',
    exchange_rate: 15.984316920438957,
    createdAt: 2019-04-22T20:06:39.513Z,
    __v: 0,
    lastUpdate: 2019-05-15T20:41:03.135Z,
    name: 'South African Rand',
    history: []
  },
  {
    _id: 5cbe1ed9cd6e6a2b47380710,
    currency: 'ARS',
    exchange_rate: 50.264175514403284,
    createdAt: 2019-04-22T20:06:49.520Z,
    __v: 0,
    lastUpdate: 2019-05-15T20:41:04.134Z,
    name: 'Argentinian Peso',
    history: []
  }
]

So I assume another array is being created, instead of returning each individual object

According to the Mongoose Update API , the correct syntax should be:

await currencies.find({}).map(currency => currencies.update({ "_id": currency._id},
  { "$push": { "history": { date: Date.now(), rate: currency.exchange_rate } } },
  function (err, raw) {
    if (err) return handleError(err);
    console.log('The raw response from Mongo was ', raw);
  }
));

Updating based on new information. May need to explicitly return the Mongoose calls and could definitely be refactored:

await currencies.find({}).map(currencyList => { 
  currencyList.forEach(currency =>  {
    currencies.updateMany({ "_id": currency._id},
      { "$push": { "history": { date: Date.now(), rate: currency.exchange_rate } } },
      function (err, raw) {
        if (err) return handleError(err);
        console.log('The raw response from Mongo was ', raw);
      }
    )
  })
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM