如何在 Mean stack 中为 crud Basic App 更新函数
How to do update Function in Mean stack For crud Basic App
我正在尝试编写一个基本的 MEAN CRUD 应用程序,但我目前停留在 CRUD 的更新部分。以下是我目前的功能。有人可以帮忙吗?
router.updateJob = function(req,res) {
var job = getByValue(jobs, req.params.id);
var oldTitle = job.title;
var newTitle = req.body.title;
job.title = newTitle;
if (oldTitle !== newTitle)
res.json({message : 'Title Updated'});
else
res.json({message : 'Title not Updated '});
};
以下是我尝试发送新标题时遇到的错误。
<h1>Cannot read property 'title' of undefined</h1>
<h2></h2>
<pre>TypeError: Cannot read property 'title' of undefined
at router.updateJob (D:\Documents\GitHub\shyft-web-app-dev-2.0\routes\job.js:48:23)
at Layer.handle [as handle_request] (D:\Documents\GitHub\shyft-web-app-dev-2.0\node_modules\express\lib\router\layer.js:95:5)
at next (D:\Documents\GitHub\shyft-web-app-dev-2.0\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (D:\Documents\GitHub\shyft-web-app-dev-2.0\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (D:\Documents\GitHub\shyft-web-app-dev-2.0\node_modules\express\lib\router\layer.js:95:5)
at D:\Documents\GitHub\shyft-web-app-dev-2.0\node_modules\express\lib\router\index.js:281:22
at param (D:\Documents\GitHub\shyft-web-app-dev-2.0\node_modules\express\lib\router\index.js:354:14)
at param (D:\Documents\GitHub\shyft-web-app-dev-2.0\node_modules\express\lib\router\index.js:365:14)
at Function.process_params (D:\Documents\GitHub\shyft-web-app-dev-2.0\node_modules\express\lib\router\index.js:410:3)
at next (D:\Documents\GitHub\shyft-web-app-dev-2.0\node_modules\express\lib\router\index.js:275:10)</pre>
最后我在下面添加了 getByValue 函数的代码
function getByValue(arr, id) {
var result = arr.filter(function(o){return o.id === id;});
return result ? result[0] : null;
}
对于给您带来的不便,我们深表歉意。
试试这个:
function getByValue(arr, id) {
var result = arr.filter(function(o){return o.id.toString() === id.toString();});
return result ? result[0] : null;
}
您遇到的错误很可能是由作业返回 undefined
引起的。至于为什么会这样,我不能确定。添加检查以查看职位和职位是否都存在以捕获错误。 @Ayush 提供的答案可能是修复 getByValue()
函数的解决方案,以便它 returns 正确的数据,具体取决于 req.params.id
的格式和 id
的键这份工作。
router.updateJob = function(req,res) {
var job = getByValue(jobs, req.params.id);
if(!job || !job.title){
return res.json({message: 'error while fetching item'})
}
var oldTitle = job.title;
var newTitle = req.body.title;
job.title = newTitle;
if (oldTitle !== newTitle)
res.json({message : 'Title Updated'});
else
res.json({message : 'Title not Updated '});
};
您没有在此处使用数组 jobs
之前定义它
var job = getByValue(jobs, req.params.id);
jobs
未定义使变量 job
未定义,这给你错误
TypeError: Cannot read property 'title' of undefined
(除非你在别处定义它,这不太可能)。
您可以使用调试器或在 var job
行的正上方插入 console.log(jobs);
来查看作业的内容。
我正在尝试编写一个基本的 MEAN CRUD 应用程序,但我目前停留在 CRUD 的更新部分。以下是我目前的功能。有人可以帮忙吗?
router.updateJob = function(req,res) {
var job = getByValue(jobs, req.params.id);
var oldTitle = job.title;
var newTitle = req.body.title;
job.title = newTitle;
if (oldTitle !== newTitle)
res.json({message : 'Title Updated'});
else
res.json({message : 'Title not Updated '});
};
以下是我尝试发送新标题时遇到的错误。
<h1>Cannot read property 'title' of undefined</h1>
<h2></h2>
<pre>TypeError: Cannot read property 'title' of undefined
at router.updateJob (D:\Documents\GitHub\shyft-web-app-dev-2.0\routes\job.js:48:23)
at Layer.handle [as handle_request] (D:\Documents\GitHub\shyft-web-app-dev-2.0\node_modules\express\lib\router\layer.js:95:5)
at next (D:\Documents\GitHub\shyft-web-app-dev-2.0\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (D:\Documents\GitHub\shyft-web-app-dev-2.0\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (D:\Documents\GitHub\shyft-web-app-dev-2.0\node_modules\express\lib\router\layer.js:95:5)
at D:\Documents\GitHub\shyft-web-app-dev-2.0\node_modules\express\lib\router\index.js:281:22
at param (D:\Documents\GitHub\shyft-web-app-dev-2.0\node_modules\express\lib\router\index.js:354:14)
at param (D:\Documents\GitHub\shyft-web-app-dev-2.0\node_modules\express\lib\router\index.js:365:14)
at Function.process_params (D:\Documents\GitHub\shyft-web-app-dev-2.0\node_modules\express\lib\router\index.js:410:3)
at next (D:\Documents\GitHub\shyft-web-app-dev-2.0\node_modules\express\lib\router\index.js:275:10)</pre>
最后我在下面添加了 getByValue 函数的代码
function getByValue(arr, id) {
var result = arr.filter(function(o){return o.id === id;});
return result ? result[0] : null;
}
对于给您带来的不便,我们深表歉意。
试试这个:
function getByValue(arr, id) {
var result = arr.filter(function(o){return o.id.toString() === id.toString();});
return result ? result[0] : null;
}
您遇到的错误很可能是由作业返回 undefined
引起的。至于为什么会这样,我不能确定。添加检查以查看职位和职位是否都存在以捕获错误。 @Ayush 提供的答案可能是修复 getByValue()
函数的解决方案,以便它 returns 正确的数据,具体取决于 req.params.id
的格式和 id
的键这份工作。
router.updateJob = function(req,res) {
var job = getByValue(jobs, req.params.id);
if(!job || !job.title){
return res.json({message: 'error while fetching item'})
}
var oldTitle = job.title;
var newTitle = req.body.title;
job.title = newTitle;
if (oldTitle !== newTitle)
res.json({message : 'Title Updated'});
else
res.json({message : 'Title not Updated '});
};
您没有在此处使用数组 jobs
之前定义它
var job = getByValue(jobs, req.params.id);
jobs
未定义使变量 job
未定义,这给你错误
TypeError: Cannot read property 'title' of undefined
(除非你在别处定义它,这不太可能)。
您可以使用调试器或在 var job
行的正上方插入 console.log(jobs);
来查看作业的内容。