Node.js 在通过array.find()的post路由中从mongoose访问数组对象时出现问题
这是我的模式Node.js 在通过array.find()的post路由中从mongoose访问数组对象时出现问题,node.js,arrays,express,mongoose,typeerror,Node.js,Arrays,Express,Mongoose,Typeerror,这是我的模式 const blogerSchema = new mongoose.Schema({ username: String, password: String, blogs: Array, }); 博客看起来像 [ { title: 'abc', content: 'abcabcabcabcabc' }, { title: 'Hello world!', content: 'hello world h
const blogerSchema = new mongoose.Schema({
username: String,
password: String,
blogs: Array,
});
博客看起来像
[
{
title: 'abc',
content: 'abcabcabcabcabc'
},
{
title: 'Hello world!',
content: 'hello world how are you. to be honest i am not.\r\n'
}
]
现在,这是我的一个app.post路线,我在这里遇到了问题
app.post("/user/:username/:title/delete", function(req, res){
const username = req.params.username;
Bloger.findOne({username: username}, function(err, foundBloger) {
const blogs = foundBloger.blogs.toObject();
// console.log(blogs.length);
blogs.find(function(post, index){
console.log(post.title);
if(post.title === req.params.title){
_.pull(blogs, post);
// Bloger.updateOne({username: username}, {blogs: blogs});
}
});
});
res.redirect("/user/"+username);
});
当我点击“提交”按钮时,该按钮会用urlhttp://localhost:3000/user/Aman/abc
。我得到这个错误
[nodemon] starting `node app.js`
Server running on port 3000
abc
events.js:292
throw er; // Unhandled 'error' event
^
TypeError: Cannot read property 'title' of undefined
at C:\The Web Devlopment Bootcamp 2020\firstBolgProject\app.js:148:24
at Array.find (<anonymous>)
at C:\The Web Devlopment Bootcamp 2020\firstBolgProject\app.js:147:11
at C:\The Web Devlopment Bootcamp 2020\firstBolgProject\node_modules\mongoose\lib\model.js:4844:16
at C:\The Web Devlopment Bootcamp 2020\firstBolgProject\node_modules\mongoose\lib\model.js:4844:16
at C:\The Web Devlopment Bootcamp 2020\firstBolgProject\node_modules\mongoose\lib\helpers\promiseOrCallback.js:24:16
at C:\The Web Devlopment Bootcamp 2020\firstBolgProject\node_modules\mongoose\lib\model.js:4867:21
at C:\The Web Devlopment Bootcamp 2020\firstBolgProject\node_modules\mongoose\lib\query.js:4476:11
at C:\The Web Devlopment Bootcamp 2020\firstBolgProject\node_modules\kareem\index.js:136:16
at processTicksAndRejections (internal/process/task_queues.js:75:11)
Emitted 'error' event on Function instance at:
at C:\The Web Devlopment Bootcamp 2020\firstBolgProject\node_modules\mongoose\lib\model.js:4846:13
at C:\The Web Devlopment Bootcamp 2020\firstBolgProject\node_modules\mongoose\lib\helpers\promiseOrCallback.js:24:16
[... lines matching original stack trace ...]
at processTicksAndRejections (internal/process/task_queues.js:75:11)
[nodemon] app crashed - waiting for file changes before starting...
[nodemon]启动`node app.js`
在端口3000上运行的服务器
abc
events.js:292
投掷者;//未处理的“错误”事件
^
TypeError:无法读取未定义的属性“title”
在C:\Web开发训练营2020\firstBolgProject\app.js:148:24
在Array.find()处
在C:\Web开发训练营2020\firstBolgProject\app.js:147:11
在C:\Web开发训练营2020\firstBolgProject\node\u modules\mongoose\lib\model.js:4844:16
在C:\Web开发训练营2020\firstBolgProject\node\u modules\mongoose\lib\model.js:4844:16
在C:\Web开发训练营2020\firstBolgProject\node\u modules\mongoose\lib\helpers\promiseOrCallback.js:24:16
在C:\Web开发训练营2020\firstBolgProject\node\u modules\mongoose\lib\model.js:4867:21
在C:\Web开发训练营2020\firstBolgProject\node\u modules\mongoose\lib\query.js:4476:11
在C:\Web开发训练营2020\firstBolgProject\node\u modules\kareem\index.js:136:16
在处理和拒绝时(内部/process/task_queues.js:75:11)
在以下位置对函数实例发出“错误”事件:
在C:\Web开发训练营2020\firstBolgProject\node\u modules\mongoose\lib\model.js:4846:13
在C:\Web开发训练营2020\firstBolgProject\node\u modules\mongoose\lib\helpers\promiseOrCallback.js:24:16
[…与原始堆栈跟踪匹配的行…]
在处理和拒绝时(内部/process/task_queues.js:75:11)
[nodemon]应用程序崩溃-正在等待文件更改,然后再启动。。。
现在这里只有event.js:292
itconsole.log(post.title)
上面的一行,但在下一行它给了我TypeError。
请调查一下。
多谢各位