These are chat archives for Automattic/mongoose

12th
Sep 2018
Joe Early
@JoeEarly
Sep 12 2018 13:53
HI all, I am trying to create a new record in the DB and when its done, update another record, but the code below is only creating the offer, and no performing the `Request¬ update.
Offers.createOffer(newOffer, function(err, newOffer) {
      if (err) {
        fs.removeSync(offerUploadPath)
        logger.error( `Failed creating offer: ${offerId}`, err )
        throw err
      }

      logger.info(`Successfully created a new offer ${newOffer.offerId}.`)

      //Remove the "req-" from the reqID
      const strippedReqId = receivedOffer.requestId.substr(6)
      //Update parent request so it has offerId
      Request.updateLinkedOfferArray(strippedReqId, receivedOffer.offerId, function(err) {
        if (err) {
          logger.error( `Failed updating request: ${receivedOffer.requestId} with offerId: ${offerId}`, err )
          throw err
        }
        logger.info(`Successfully update request: ${receivedOffer.requestId} with new offerId: ${receivedOffer.offerId}.`)
      })

    })
module.exports.updateLinkedOfferArray = function(requestId, offerId) {
  const query = { uuid: requestId }
  Request.updateOne({ query }, { $push: { linkedOffers: offerId } }, function(
    err,
    raw
  ) {
    console.log("The raw response from Mongo was ", raw)
  })
}
Kev
@lineus
Sep 12 2018 14:35
@AndreyGazhala sorry, my raid array fell apart yesterday and left me without a machine to use. It looks like you're using $elemMatch on a string, but $elemMatch is for arrays.
@JoeEarly your updateLinkedOfferArray is written to take 2 parameters, but you're passing 3 in. Do you mean to pass the function through to the updateOne method?