尝试创建 post 请求时出现 Express js 错误
Express js error while trying to create a post request
我试图创建一个 post 请求以创建一个“要约”。
不幸的是,每次我尝试发送数据时,它都会返回一个错误,我不知道为什么它不起作用!有人可以帮我吗?
这是我的快递API
router.post('/create-offer', (req, res) => {
Offer = new Offer({
Title: req.body.Title,
Position: req.body.Position,
Location: req.body.Location,
Duration: req.body.Duration,
WorkingHours: req.body.WorkingHours
// OfferDescription: req.body.OfferDescription,
// Tags: req.body.Tags,
})
Offer
.save()
.then(data => {
res.json(data);
})
.catch(err => {
res.status(500).send({
message:
err.message || "Some error occurred while creating the Offer."
});
});
return res.send('Received a POST HTTP method');
})
这是我的 mongodb 模型:
我总是收到错误 500
试试这样的东西:
router.post('/offer', (req, res) => { // create-offer -> offer
const offer = new Offer({ // offer -> Offer
Title: req.body.Title,
Position: req.body.Position,
Location: req.body.Location,
Duration: req.body.Duration,
WorkingHours: req.body.WorkingHours
})
return offer
.save()
.then(data => {
return res.json(data);
})
.catch(err => {
res.status(500).json({
message:
err.message || "Some error occurred while creating the Offer."
});
});
});
// remove res.send('...'), you can only give one response, using `res.send` and `res.json` will send two responses.
我试图创建一个 post 请求以创建一个“要约”。 不幸的是,每次我尝试发送数据时,它都会返回一个错误,我不知道为什么它不起作用!有人可以帮我吗?
这是我的快递API
router.post('/create-offer', (req, res) => {
Offer = new Offer({
Title: req.body.Title,
Position: req.body.Position,
Location: req.body.Location,
Duration: req.body.Duration,
WorkingHours: req.body.WorkingHours
// OfferDescription: req.body.OfferDescription,
// Tags: req.body.Tags,
})
Offer
.save()
.then(data => {
res.json(data);
})
.catch(err => {
res.status(500).send({
message:
err.message || "Some error occurred while creating the Offer."
});
});
return res.send('Received a POST HTTP method');
})
这是我的 mongodb 模型:
我总是收到错误 500
试试这样的东西:
router.post('/offer', (req, res) => { // create-offer -> offer
const offer = new Offer({ // offer -> Offer
Title: req.body.Title,
Position: req.body.Position,
Location: req.body.Location,
Duration: req.body.Duration,
WorkingHours: req.body.WorkingHours
})
return offer
.save()
.then(data => {
return res.json(data);
})
.catch(err => {
res.status(500).json({
message:
err.message || "Some error occurred while creating the Offer."
});
});
});
// remove res.send('...'), you can only give one response, using `res.send` and `res.json` will send two responses.