Click here to Skip to main content
15,502,871 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I'm trying to switch from the first code to the second one I hope you can guide me.



speak more clearly

I'm trying to change the first 'req.body.dharma'

to the second 'return async (req, res) => {     let article = req.article     article.title = req.body.title     article.description = req.body.description     article.markdown = req.body.markdown'.


What I have tried:

first code:

module.exports.updateDharma = async (req, res) => {
    const { id } = req.params;
    console.log(req.body);
    const dharma = await Dharma.findByIdAndUpdate(id, { ...req.body.dharma });
    const imgs = req.files.map(f => ({ url: f.path, filename: f.filename }));
    dharma.images.push(...imgs);
    await dharma.save()
    if(req.body.deleteImages){
        for(let filename of req.body.deleteImages){
            await cloudinary.uploader.destroy(filename);
        }
        await dharma.updateOne({ $pull: { images: {filename: { $in: req.body.deleteImages } } } } )
    }
    req.flash('infoDharma', 'อัปเดตสำเร็จ');
    res.redirect(`${dharma._id}`)
}
second code:

function saveArticleAndRedirect(path) {
  return async (req, res) => {
    let article = req.article
    article.title = req.body.title
    article.description = req.body.description
    article.markdown = req.body.markdown
    try {
      article = await article.save()
      res.redirect(`/articles/${article.slug}`)
    } catch (e) {
      res.render(`articles/${path}`, { article: article })
    }
  }
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900