0

I have this and I cannot get it to work. Console.log logs null. If I log before de update code it actually logs the item that im trying to edit. If I do it after the findOneAndUpdate() it returns null.

Edit 2:

I changed the mongoose method to findById and it worked. Still have one problem though. New code:

let promo = await Promo.findById(req.body._id)
      console.log('Promo findById(promoId) => ' + promo)
      console.log('Promo Fields => ' + JSON.stringify(promoFields))
      if (promo) {
        // Update
        const promo = await Promo.findOneAndUpdate(
          { promo: req.body_id },
          { $set: promoFields },
          { new: true },
          (err, doc) =>
            console.log(err + ' <--- err ///// ' + doc + ' <--- doc ')
        )
        return res.json(promo)

Output:

[0] Promo findById(promoId) => { _id: 5cee8b0134349e1eec482053,
[0]   title: 'Dos',
[0]   subtitle: 'dos',
[0]   desc: 'dos',
[0]   __v: 0 }
[0] Promo Fields => {"title":"Dos","subtitle":"dos","desc":"dos"}
[0] null <--- err ///// { _id: 5cee6bcbcfd3f11d580aa715,
[0]   new: false,
[0]   desc: 'dos',
[0]   subtitle: 'dos',
[0]   title: 'Dos' } <--- doc

The problem now is no matter what item from the list I edit it always updates de first one. Can it be an id mismatch? If I debug the id from req, I have the correct id from the item selected.. don't know why it behaves this way. Last but not least here's the reducer:

const initialState = {
  promos: null
}

export default function(state = initialState, action) {
  const { type, payload } = action

  switch (type) {
    case POST_PROMOS:
      return {
        ...state,
        ...payload,
        loading: false
      }
    default:
      return state
  }
}

I was wondering if I could in the reduce make something like

state.promos.filter(promo => promo._id !== payload)

and then generate a new array with the payload item. I'm mixing concepts?

Thank you in advance

23
  • What is the promoFields referring to? Commented May 28, 2019 at 13:48
  • My bad @developing2020, already edited. Commented May 28, 2019 at 13:50
  • I read that it logs the item before the update. So can you add the output of a the console.log(promo) after the findOne? Commented May 28, 2019 at 14:03
  • @JackRed If I ran it before the if(promo){ line, it logs the actual item Commented May 28, 2019 at 14:11
  • @JackRed and if I return res.json(promo), console.log(promo) all in the same line, logging it after, it keeps null Commented May 28, 2019 at 14:12

2 Answers 2

1

Bingo! It seems that req.body._id for some reason even though it was the Id from the desired editing item, it was not an ObjectId.

I let promoId = mongoDb.ObjectId(req.body._id) and then

promo = await Promo.findOneAndUpdate(
          { _id: promoId },
          { $set: promoFields },
          { new: true }
        )

:)! Thanks

Sign up to request clarification or add additional context in comments.

Comments

0

You should use $addToSet in place of $set and it will maybe work.

2 Comments

Same output.. null. Can it be the models id?
do u debugg? maybe u missing some thing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.