0
votes
let { id, email } = req.body;

const findUser = await User.findOne({ email: email })
if (!findUser)
    return res.status(400).json({ message: 'No receiver id has been found' })

const existingContacts = findUser.contacts;
const checkId = obj => obj.id === id;
if (existingContacts.some(checkId))
    return res.status(400).json({ message: "Contact already exist" })

const newContact = await User.findById(id)

const newId = await User.findOneAndUpdate({ email: email }, {
    $push: {
        contacts: {
            id: id,
            name: newContact.firstName
        }
    }
})
res.json(newId)

When i send a post request i get the following error:

Cast to ObjectId failed for value "2" at path "_id" for model "User"

any suggestion?

thanks in advance!!!

1

1 Answers

0
votes

The value 2, doesn’t match the mongoose model _id