英文:
how to render a object an ejs template
问题
I am trying to render a trip object in an EJS template, but, I am getting the error
"ReferenceError: trip is not defined."
I have passed the trip object to the EJS template as a parameter, and I have checked the spelling of the variable name. I don't know why I am getting this error.
Here is the code that I am using:
const trip = {type type: "national",
departure: "Paris",
destination: "Rome",
departureTime: "10:00 AM",
price: 100
};
res.render('companyDashboard', { trip, company: req.user.company });
Here is the EJS template that I am using:
<p class="card-text">
<strong>Company :</strong> <%= company.company %><br>
<strong>Type :</strong> <%= trip.type %><br>
<strong>Departure :</strong> <%= trip.departure %><br>
<strong>Destination :</strong> <%= trip.destination %><br>
<strong>Departure Times :</strong>
<ul>
<% trip.departureTime.forEach(time => { %>
<li><%= time %></li>
<% }); %>
</ul>
<strong>Price :</strong> <%= trip.price %>
</p>
I would appreciate any help that you can provide.
I tried to render a trip object in an EJS template. I have expected the trip object to be rendered in the template, but, I got an error instead.
Here's what I tried:
res.render('companyDashboard', { trip, company: req.user.company });
The trip object would be rendered in the companyDashboard template.
I got an error: ReferenceError: trip is not defined
.
英文:
I am trying to render a trip object in an EJS template, but, I am getting the error
> "ReferenceError: trip is not defined."
I have passed the trip object to the EJS template as a parameter, and I have checked the spelling of the variable name. I don't know why I am getting this error.
Here is the code that I am using:
const trip = {type type: "national",
departure: "Paris",
destination: "Rome",
departureTime: "10:00 AM",
price: 100
};here
res.render('companyDashboard', { trip, company: req.user.company });
Here is the EJS template that I am using:
<p class="card-text">
<strong>Company :</strong> <%= company.company %><br>
<strong>Type :</strong> <%= trip.type %><br>
<strong>Departure :</strong> <%= trip.departure %><br>
<strong>Destination :</strong> <%= trip.destination %><br>
<strong>Departure Times :</strong>
<ul>
<% trip.departureTime.forEach(time => { %>
<li><%= time %></li>
<% }); %>
</ul>
<strong>Price :</strong> <%= trip.price %>
</p>
I would appreciate any help that you can provide.
I tried to render a trip object in an EJS template. I have expected the trip object to be rendered in the template, but, I got an error instead.
Here's what I tried:
res.render('companyDashboard', { trip, company: req.user.company });
The trip object would be rendered in the companyDashboard template.
I got an error: ReferenceError: trip is not defined
.
答案1
得分: 1
I noticed two issues in your code:
在你的代码中我注意到了两个问题:
In the object declaration, you have a syntax error on the first line. You have written type type: "national"
where it should be type: "national"
.
在对象声明中,第一行存在语法错误。你写成了 type type: "national"
,应该改为 type: "national"
。
In the EJS template, you are trying to use forEach
on trip.departureTime
as if it is an array, but trip.departureTime
is a string, not an array.
在 EJS 模板中,你尝试在 trip.departureTime
上使用 forEach
,好像它是一个数组,但 trip.departureTime
实际上是一个字符串,而不是数组。
So first correct the object declaration:
因此,首先纠正对象声明:
const trip = {
type: "national",
departure: "Paris",
destination: "Rome",
departureTime: "10:00 AM",
price: 100
};
Then, depending on your requirements:
然后,根据你的需求:
If departureTime
is supposed to be a single time string, remove the forEach
in the EJS template:
如果 departureTime
应该是一个单独的时间字符串,在 EJS 模板中删除 forEach
:
<strong>Departure Time :</strong> <%= trip.departureTime %><br>
If departureTime
is supposed to be an array of times, change it to an array:
如果 departureTime
应该是一个时间数组,将其改为数组:
const trip = {
type: "national",
departure: "Paris",
destination: "Rome",
departureTime: ["10:00 AM", "02:00 PM", "06:00 PM"],
price: 100
};
And keep the forEach
in the EJS template.
然后在 EJS 模板中保留 forEach
。
英文:
From your question, I noticed two issues in your code:
In the object declaration, you have a syntax error on the first line. You have written type type: "national"
where it should be type: "national"
.
In the EJS template, you are trying to use forEach
on trip.departureTime
as if it is an array, but trip.departureTime
is a string, not an array.
So first correct the object declaration:
const trip = {
type: "national",
departure: "Paris",
destination: "Rome",
departureTime: "10:00 AM",
price: 100
};
Then, depending on your requirements:
If departureTime
is supposed to be a single time string, remove the forEach
in the EJS template:
<strong>Departure Time :</strong> <%= trip.departureTime %><br>
If departureTime
is supposed to be an array of times, change it to an array:
const trip = {
type: "national",
departure: "Paris",
destination: "Rome",
departureTime: ["10:00 AM", "02:00 PM", "06:00 PM"],
price: 100
};
And keep the forEach
in the EJS template.
答案2
得分: 0
这里是控制器部分的翻译:
exports.createTrip = async (req, res) => {
try {
const { type, departure, destination, departureTime, price } = req.body;
const trip = new Trip({
company: req.user._id,
type,
routes: [
{
departure,
destination,
departureTime,
price
}
]
});
await trip.save();
req.flash('success_msg', 'Trajet créé');
console.log(trip)
console.log(req.user.company)
res.render('companyDashboard', { trip, company: req.user.company });
} catch (error) {
console.log(error);
req.flash('error_msg', 'Une erreur s\'est produite lors de la soumission du trajet');
res.redirect('/company/tripForm');
}
};
以下是ejs代码部分的翻译:
<div class="container">
<h1 class="text-center me-3">Tableau de bord de <span class="text-primary"><%= company.company %></span></h1>
<div class="row d-flex">
<div>
<%- include('tripForm') %>
</div>
<div class="card">
<div class="card-header">
Trajet soumis
</div>
<div class="card-body">
<h5 class="card-title">Informations du trajet</h5>
<p class="card-text">
<strong>Compagnie :</strong> <%= company.company %><br>
<strong>Type :</strong> <%= trip.type %><br>
<strong>Départ :</strong> <%= trip.routes[0].departure %><br>
<strong>Destination :</strong> <%= trip.routes[0].destination %><br>
<strong>Heures de départ :</strong>
<ul>
<% trip.routes[0].departureTime.forEach(time => { %>
<li><%= time %></li>
<% }); %>
</ul>
<strong>Prix :</strong> <%= trip.routes[0].price %>
</p>
</div>
</div>
</div>
</div>
希望这些翻译对您有所帮助。如果您需要更多帮助,请随时告诉我。
英文:
Here the controller:
exports.createTrip = async (req, res) => {
try {
const { type, departure, destination, departureTime, price } = req.body;
const trip = new Trip({
company: req.user._id,
type,
routes: [
{
departure,
destination,
departureTime,
price
}
]
});
await trip.save();
req.flash('success_msg', 'Trajet créé');
console.log(trip)
console.log(req.user.company)
res.render('companyDashboard', { trip, company: req.user.company });
} catch (error) {
console.log(error);
req.flash('error_msg', 'Une erreur s\'est produite lors de la soumission du trajet');
res.redirect('/company/tripForm');
}
};
And the ejs code:
<div class="container">
<h1 class="text-center me-3">Tableau de bord de <span class="text-primary"><%= company.company %> </span> </h1>
<div class="row d-flex">
<div>
<%- include('tripForm') %>
</div>
<div class="card">
<div class="card-header">
Trajet soumis
</div>
<div class="card-body">
<h5 class="card-title">Informations du trajet</h5>
<p class="card-text">
<strong>Compagnie :</strong> <%= company.company %><br>
<strong>Type :</strong> <%= trip.type %><br>
<strong>Départ :</strong> <%= trip.routes[0].departure %><br>
<strong>Destination :</strong> <%= trip.routes[0].destination %><br>
<strong>Heures de départ :</strong>
<ul>
<% trip.routes[0].departureTime.forEach(time => { %>
<li><%= time %></li>
<% }); %>
</ul>
<strong>Prix :</strong> <%= trip.routes[0].price %>
</p>
</div>
</div>
</div>
</div>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论