邮递员错误:TypeError:无法将属性	username	设置为null
邮递员错误:TypeError:无法将属性 username 设置为null
我一直在处理相同的错误,代码可以很好地用于记录的添加,获取和删除,但是当我尝试发送发布更新请求时,在Postman上引发了错误。邮差和失眠都普遍存在相同的错误。ct router = require("express").Router();
let Exercise = require("../m
邮递员错误:TypeError:无法将属性 username 设置为null
我一直在处理相同的错误,代码可以很好地用于记录的添加,获取和删除,但是当我尝试发送发布更新请求时,在Postman上引发了错误。邮差和失眠都普遍存在相同的错误。
ct router = require("express").Router();
let Exercise = require("../models/");
router.route("/").get((req, res) => {
Exercise.find()
.then(exercises => res.json(exercises))
.catch(err => res.status(400).json("Error: " + err));
});
router.route("/add").post((req, res) => {
ct username = req.body.username;
ct description = req.body.description;
ct duration = umber(req.body.duration);
ct date = Date.parse(req.body.date);
ct newExercise = new Exercise({
username,
description,
duration,
date
});
newExercise
.save()
.then(() =>
res
.json("Exercise added!")
.catch(err => res.status(400).json("Error: " + err))
);
});
router.route("/:id").get((req, res) => {
Exercise.findById(req.params.id)
.then(exercise => res.json(exercise))
.catch(err => res.status(400).json("Error: " + err));
});
router.route("/:id").delete((req, res) => {
Exercise.findByIdAndDelete(req.params.id)
.then(() => res.json("Exercise deleted."))
.catch(err => res.status(400).json("Error: " + err));
});
router.route("/update/:id").post((req, res) => {
Exercise.findById(req.params.id)
.then(exercise => {
exercise.username = req.body.username;
exercise.description = req.body.description;
exercise.duration = umber(req.body.duration);
exercise.date = Date.parse(req.body.date);
exercise
.save()
.then(() => res.json("Exercise Updated!"))
.catch(err => res.status(400).json("Error: " + err));
})
.catch(err => res.status(400).json("Error: " + err));
});
= router;
邮递员中的请求正文是基于简单应用程序/ json的邮寄请求:
{
"username": "Omer Mustafa",
"description": "Walk",
"duration": "120",
"date": "2019-10-07T11:19:09.424Z"
}
我正在按照本教程进行实验,以构建一个简单的mern应用程序:
回答如下:mongoose FindById如果不存在数据,则不会引发异常,但是会返回null。因此,在使用之前,您需要检查数据是否不为null。您只需要添加此检查:
if (!exercise) return res.status(404).send();
所以所有与更新相关的代码现在看起来:
router.route("/update/:id").post((req, res) => {
Exercise.findById(req.params.id)
.then(exercise => {
if (!exercise) return res.status(404).send();
exercise.username = req.body.username;
exercise.description = req.body.description;
exercise.duration = umber(req.body.duration);
exercise.date = Date.parse(req.body.date);
exercise
.save()
.then(() => res.json("Exercise Updated!"))
.catch(err => res.status(400).json("Error: " + err));
})
.catch(err => res.status(400).json("Error: " + err));
});
#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
上传时间: 2024-04-17 06:56:32
上一篇:uni项目运行到模拟器
推荐阅读
留言与评论(共有 6 条评论) |
本站网友 南水北调西线工程 | 30分钟前 发表 |
id").get((req | |
本站网友 心急如焚 | 15分钟前 发表 |
id").post((req | |
本站网友 紫金矿业董事长 | 24分钟前 发表 |
在Postman上引发了错误 | |
本站网友 文东影院 | 3分钟前 发表 |
但是会返回null | |
本站网友 无创去皱 | 29分钟前 发表 |
res) => { ct username = req.body.username; ct description = req.body.description; ct duration = umber(req.body.duration); ct date = Date.parse(req.body.date); ct newExercise = new Exercise({ username |