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
promoFieldsreferring to?if(promo){line, it logs the actual itemreturn res.json(promo), console.log(promo)all in the same line, logging it after, it keeps null