how to change an object to array in postman in order to solve "TypeError: tasks.map is not a function"
how to change an object to array in postman in order to solve "TypeError: tasks.map is not a function"
我正在创建一个 api,应该使用 multer 上传图片,但我得到 tasks.map 不是一个函数,这里是代码:
router.post('/',uploads.any(), async (req, res) => {
try{
const { title, description, tasks } = req.body
const project = await Project.create({title, description, user: req.userId });
console.log(req.body)
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'})
}
})
我的 console.log 对于 req.body 是这样的:
[Object: null prototype] {
title: 'titulo',
description: 'tinha feito errado',
tasks: [Object: null prototype] {
title: 'testee form',
descriptiom: 'tomara que funcione',
dicionario: [Object: null prototype] { nome: 'Teste' },
assignedTo: '605df6eab581542e58d447ac'
}
}
你怎么看出它是一个对象,但我需要它是一个数组,我该如何解决这个问题?
这是我的要求:
您的 tasks
对象不是数组(正如您在 console.log()
中看到的那样),因此您不能在其上使用 map
。
如果你想把它作为一个数组发送,尝试在 Postman 中这样发送数据:
tasks[0][title]
tasks[0][description]
...
因此,只需在 tasks
之后添加 [0]
。
我正在创建一个 api,应该使用 multer 上传图片,但我得到 tasks.map 不是一个函数,这里是代码:
router.post('/',uploads.any(), async (req, res) => {
try{
const { title, description, tasks } = req.body
const project = await Project.create({title, description, user: req.userId });
console.log(req.body)
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'})
}
})
我的 console.log 对于 req.body 是这样的:
[Object: null prototype] {
title: 'titulo',
description: 'tinha feito errado',
tasks: [Object: null prototype] {
title: 'testee form',
descriptiom: 'tomara que funcione',
dicionario: [Object: null prototype] { nome: 'Teste' },
assignedTo: '605df6eab581542e58d447ac'
}
}
你怎么看出它是一个对象,但我需要它是一个数组,我该如何解决这个问题?
这是我的要求:
您的 tasks
对象不是数组(正如您在 console.log()
中看到的那样),因此您不能在其上使用 map
。
如果你想把它作为一个数组发送,尝试在 Postman 中这样发送数据:
tasks[0][title]
tasks[0][description]
...
因此,只需在 tasks
之后添加 [0]
。