【问题标题】:Cannot read property 'map' of undefined in mongodb无法读取 mongodb 中未定义的属性“地图”
【发布时间】:2021-04-08 19:09:55
【问题描述】:

我正在为一个项目创建一个在 mongo 中上传图片的 api,但我收到以下错误:

TypeError: Cannot read property 'map' of undefined
    at C:\Users\Usuário\Desktop\dicionario\backend\src\app\controllers\ProjectController.js:58:32
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

错误是地图中的第 58 行

router.post('/', uploads.any() ,async (req, res) => {
    console.log(req.file)
    try{
        const { title, description, tasks } = req.body

        const project =  await Project.create({title, description, user: req.userId });

       await Promise.all(tasks.map( async task => {
            const projectTask = new Task({...task, project: project._id})

           await projectTask.save()

           project.tasks.push(projectTask)
        }))

        await project.save()
        return res.send({ project })
    } catch(err) {
        console.log(err)
        return res.status(400).send({error: 'Error creating new project'})
        
       
    }
})

我的 req 带有一个 console.log:

   title: 'titulo',
   description: 'tinha feito errado',
  'tasks.title': 'testee form',
  'tasks[][description]': 'tomara que funcione',
  'tasks[][dicionario[] [nome]]': 'Teste',
  'tasks[][assignedTo]': '605df6eab581542e58d447ac'
}

但我不知道如何解决这个问题,你能帮我吗?

【问题讨论】:

  • tasks 可能没有作为req.body 的成员传递? (可以通过console.log查看)

标签: javascript node.js mongodb multer


【解决方案1】:

尝试console.log(req.body) 并检查您是否收到tasks。错误说tasksundefined

【讨论】:

  • 用req.body编辑的,可以看看吗?
猜你喜欢
  • 2021-11-11
  • 1970-01-01
  • 1970-01-01
  • 2022-01-14
  • 1970-01-01
  • 2021-09-30
  • 2017-11-10
  • 2017-07-30
  • 2020-09-14
相关资源
最近更新 更多