<% for(var i = 0; i< Homeposts.length; i++){%> <% console.log(Homeposts.title[i]) %> <%}%>
`TypeError: /Users/sheshureddy/Desktop/challenge/views/home.ejs:6
4| <br>
5| <% for(var i = 0; i< Homeposts.length; i++){%>
>> 6| <% console.log(Homeposts.title[i]) %>
7| <%}%>
8| <%- include('partials/footer'); -%>
Cannot read properties of undefined (reading '0')
at eval ("/Users/sheshureddy/Desktop/challenge/views/home.ejs":19:35)
at home (/Users/sheshureddy/Desktop/challenge/node_modules/ejs/lib/ejs.js:703:17)
at tryHandleCache (/Users/sheshureddy/Desktop/challenge/node_modules/ejs/lib/ejs.js:274:36)
at exports.renderFile [as engine] (/Users/sheshureddy/Desktop/challenge/node_modules/ejs/lib/ejs.js:491:10)
at View.render (/Users/sheshureddy/Desktop/challenge/node_modules/express/lib/view.js:135:8)
at tryRender (/Users/sheshureddy/Desktop/challenge/node_modules/express/lib/application.js:657:10)
at Function.render (/Users/sheshureddy/Desktop/challenge/node_modules/express/lib/application.js:609:3)
at ServerResponse.render (/Users/sheshureddy/Desktop/challenge/node_modules/express/lib/response.js:1039:7)
at /Users/sheshureddy/Desktop/challenge/app.js:15:7
at Layer.handle [as handle_request] (/Users/sheshureddy/Desktop/challenge/node_modules/express/lib/router/layer.js:95:5)`
我刚刚把我的对象数据从我的js文件传递到我的ejs文件,我试图通过对象成员数据来console.log。但它显示错误。
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const posts = [];
const homeStartingContent = "Lacus vel facilisis volutpat est velit egestas dui id ornare. Semper auctor neque vitae tempus quam. Sit amet cursus sit amet dictum sit amet justo. Viverra tellus in hac habitasse. Imperdiet proin fermentum leo vel orci porta. Donec ultrices tincidunt arcu non sodales neque sodales ut. Mattis molestie a iaculis at erat pellentesque adipiscing. Magnis dis parturient montes nascetur ridiculus mus mauris vitae ultricies. Adipiscing elit ut aliquam purus sit amet luctus venenatis lectus. Ultrices vitae auctor eu augue ut lectus arcu bibendum at. Odio euismod lacinia at quis risus sed vulputate odio ut. Cursus mattis molestie a iaculis at erat pellentesque adipiscing.";
const aboutContent = "Hac habitasse platea dictumst vestibulum rhoncus est pellentesque. Dictumst vestibulum rhoncus est pellentesque elit ullamcorper. Non diam phasellus vestibulum lorem sed. Platea dictumst quisque sagittis purus sit. Egestas sed sed risus pretium quam vulputate dignissim suspendisse. Mauris in aliquam sem fringilla. Semper risus in hendrerit gravida rutrum quisque non tellus orci. Amet massa vitae tortor condimentum lacinia quis vel eros. Enim ut tellus elementum sagittis vitae. Mauris ultrices eros in cursus turpis massa tincidunt dui.";
const contactContent = "Scelerisque eleifend donec pretium vulputate sapien. Rhoncus urna neque viverra justo nec ultrices. Arcu dui vivamus arcu felis bibendum. Consectetur adipiscing elit duis tristique. Risus viverra adipiscing at in tellus integer feugiat. Sapien nec sagittis aliquam malesuada bibendum arcu vitae. Consequat interdum varius sit amet mattis. Iaculis nunc sed augue lacus. Interdum posuere lorem ipsum dolor sit amet consectetur adipiscing elit. Pulvinar elementum integer enim neque. Ultrices gravida dictum fusce ut placerat orci nulla. Mauris in aliquam sem fringilla ut morbi tincidunt. Tortor posuere ac ut consequat semper viverra nam libero.";
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
app.get("/", function (req, res) {
res.render('home', { text: homeStartingContent,Homeposts:posts})
})
app.get("/about", function (req, res) {
res.render('about', { aboutText: aboutContent });
});
app.get("/contact", function (req, res) {
res.render('contact', { ContactText: contactContent });
})
app.get("/compose", function (req, res) {
res.render('compose');
});
app.post('/compose', function (req, res) {
let pos = {
title: req.body.PostTitle,
content: req.body.PostBody
};
const post = JSON.stringify(pos);
posts.push(post);
res.redirect("/");
});
app.listen(3000, function () {
console.log("Server started on port 3000");
});
所以这是我上面的app.js代码和ejs文件,请给予我一些建议。我想知道为什么我的代码不工作。我面临的错误,因为过去1小时。
1条答案
按热度按时间58wvjzkj1#
它是
Homeposts[i].title
,将索引移动到属性之前:另外,不要将对象转换为字符串,只需将对象推送到posts数组,否则将无法访问属性。
所以,去掉这条线
然后按
pos
:或者更短: