Answer the question
In order to leave comments, you need to log in
Cannot read property 'length' of undefined Node JS?
What to do with template error
/home/bred/Рабочий стол/Папка/Blog/views/post/post.ejs:152 150| <h3>Комментарии</h3> 151| <div class="comment-list"> >> 152| <%- include('../layout/comments.ejs', {items: comments}) %> 153| <button class="btn btn-link" id="new">Ответить</button> 154| <form class="comment" action=""> 155| <textarea class="form-control" name="body" id="" rows="3"></textarea> /home/bred/Рабочий стол/Папка/Blog/views/layout/comments.ejs:12 10| <%= item.body %> 11| <span class="link" id="reply">ответить</span> >> 12| <%- include('./comments.ejs', {items: item.children}) %> 13| </li> 14| <% }) %> 15| /home/bred/Рабочий стол/Папка/Blog/views/layout/comments.ejs:3 1| Comments 2| >> 3| <% if (items.length) { %> 4| <ul> 5| <% } %> 6| Cannot read property 'length' of undefined
TypeError: /home/bred/Рабочий стол/Папка/Blog/views/post/post.ejs:152
150| <h3>Комментарии</h3>
151| <div class="comment-list">
>> 152| <%- include('../layout/comments.ejs', {items: comments}) %>
153| <button class="btn btn-link" id="new">Ответить</button>
154| <form class="comment" action="">
155| <textarea class="form-control" name="body" id="" rows="3"></textarea>
/home/bred/Рабочий стол/Папка/Blog/views/layout/comments.ejs:12
10| <%= item.body %>
11| <span class="link" id="reply">ответить</span>
>> 12| <%- include('./comments.ejs', {items: item.children}) %>
13| </li>
14| <% }) %>
15|
/home/bred/Рабочий стол/Папка/Blog/views/layout/comments.ejs:3
1| Comments
2|
>> 3| <% if (items.length) { %>
4| <ul>
5| <% } %>
6|
Cannot read property 'length' of undefined
at eval (eval at compile (/home/bred/Рабочий стол/Папка/Blog/node_modules/ejs/lib/ejs.js:633:12), <anonymous>:11:18)
at returnedFn (/home/bred/Рабочий стол/Папка/Blog/node_modules/ejs/lib/ejs.js:668:17)
at include (/home/bred/Рабочий стол/Папка/Blog/node_modules/ejs/lib/ejs.js:666:39)
at eval (eval at compile (/home/bred/Рабочий стол/Папка/Blog/node_modules/ejs/lib/ejs.js:633:12), <anonymous>:26:17)
at Array.forEach (<anonymous>)
at eval (eval at compile (/home/bred/Рабочий стол/Папка/Blog/node_modules/ejs/lib/ejs.js:633:12), <anonymous>:17:14)
at returnedFn (/home/bred/Рабочий стол/Папка/Blog/node_modules/ejs/lib/ejs.js:668:17)
at include (/home/bred/Рабочий стол/Папка/Blog/node_modules/ejs/lib/ejs.js:666:39)
at eval (eval at compile (/home/bred/Рабочий стол/Папка/Blog/node_modules/ejs/lib/ejs.js:633:12), <anonymous>:60:17)
at returnedFn (/home/bred/Рабочий стол/Папка/Blog/node_modules/ejs/lib/ejs.js:668:17)
at tryHandleCache (/home/bred/Рабочий стол/Папка/Blog/node_modules/ejs/lib/ejs.js:254:36)
at View.exports.renderFile [as engine] (/home/bred/Рабочий стол/Папка/Blog/node_modules/ejs/lib/ejs.js:485:10)
at View.render (/home/bred/Рабочий стол/Папка/Blog/node_modules/express/lib/view.js:135:8)
at tryRender (/home/bred/Рабочий стол/Папка/Blog/node_modules/express/lib/application.js:640:10)
at Function.render (/home/bred/Рабочий стол/Папка/Blog/node_modules/express/lib/application.js:592:3)
at ServerResponse.render (/home/bred/Рабочий стол/Папка/Blog/node_modules/express/lib/response.js:1012:7)
<% include ../layout/header %>
<% if (!user.id && !user.login) { %>
<!-- Posts { -->
</div>
<div class="col-md-8 order-md-1">
<div class="card mb-2" style="">
<div class="card-body">
<h5 class="card-title"><%= post.title %></h5>
<p class="card-text"><%= post.body %></p>
<h6 class="card-subtitle mb-2 text-muted"><b>Создано: </b><%= post.createdAt %><br><b>Обновлено: </b><%= post.updatedAt %></h6>
<span>
Автор:<a href="#" class="card-link"> <%= post.author %></a>
</span>
<a href="#" class="card-link float-right">Комментарии</a>
</div>
</div>
</div>
<!-- } -->
<div class="comments" id="<%= post.id %>">
<h3>Комментарии</h3>
<div class="comment-list">
<%- include('../layout/comments.ejs', {items: comments}) %>
<button class="btn btn-link" id="new">Ответить</button>
<form class="comment" action="">
<textarea class="form-control" name="body" id="" rows="3"></textarea>
<div class="buttons">
<button class="btn btn-danger cancel">Закрыть</button>
<button class="btn btn-primary send">Отправить</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<% include ../layout/footer %>
router.get('/posts/:post', async (req, res, next) => {
const url = req.params.post.trim().replace(/ +(?= )/g, '');
const userId = req.session.userId;
const userLogin = req.session.userLogin;
if (!url) {
const err = new Error('Not Found');
err.status = 404;
next(err);
} else {
try {
const post = await models.Post.findOne({
url
});
if (!post) {
const err = new Error('Not Found');
err.status = 404;
next(err);
} else {
const comments = await models.Comment.find({
post: post.id
})
console.log(comments)
res.render('post/post', {
post,
comments,
user: {
id: userId,
login: userLogin
}
});
}
} catch (error) {
throw new Error('Server Error');
}
}
});
router.post('/add', async (req, res) => {
const userId = req.session.userId;
const userLogin = req.session.userLogin;
console.log(req.body)
if (!userId || !userLogin) {
res.json({
ok: false,
});
} else {
const post = req.body.post;
const body = req.body.body;
const parent = req.body.parent;
try {
if (!parent) {
await models.Comment.create({
post,
body,
author: userId
})
}
} catch (error) {
res.json({
ok: false,
});
}
}
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question