使用 hbs 循环 API
Looping through API with hbs
我来自ejs。我有一个循环遍历我的 API 数据的函数,如下所示:
<p><% data["Search"].forEach(function(movies){ %>
<li><strong><%= movies["Title"] %></strong> - <%= movies["Year"] %> - <%= movies["Type"] %></li>
<% }) %></p>
我如何使用 hbs 来做同样的事情?我试过这个:
<div>
{{#each data}}
<div class="row">
<div class="col-md-12">
{{Title}}
</div>
</div>
{{/each}}
</div>
但它没有显示任何内容。
这是我的app.js:
const express = require("express"),
request = require("request"),
bodyParser = require("body-parser");
var app = express();
app.use(express.static("public"));
app.set("view engine", "hbs");
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/moviesearch", function(req, res){
res.render("moviesearch");
});
app.get("/movieResults", function (req, res) {
var user = req.query.getValue;
var url = "http://www.omdbapi.com/?s=" + user + "&apikey=...";
request(url, function (error, response, body) {
var data = JSON.parse(body);
res.render("API_Results", { data: data });
});
});
如何在 API_Result.hbs 上循环遍历这些值?
搜索示例:扫帚
{
Search: [
{
Title: "Bedknobs and Broomsticks",
Year: "1971",
imdbID: "tt0066817",
Type: "movie",
Poster: "https://m.media-amazon.com/images/M/MV5BMTUxMTY3MTE5OF5BMl5BanBnXkFtZTgwNTQ0ODgxMzE@._V1_SX300.jpg",
},
{
Title: "When Broomsticks Were King",
Year: "2001",
imdbID: "tt1418965",
Type: "movie",
Poster: "N/A",
},
],
totalResults: "5",
Response: "True",
}
谢谢
请查看以下代码。你必须像我们在 js 中那样 运行 一个循环。
<div>
{{#each data.Search}}
<div class="row">
<div class="col-md-12">
{{Title}}
</div>
</div>
{{/each}}
</div>
我来自ejs。我有一个循环遍历我的 API 数据的函数,如下所示:
<p><% data["Search"].forEach(function(movies){ %>
<li><strong><%= movies["Title"] %></strong> - <%= movies["Year"] %> - <%= movies["Type"] %></li>
<% }) %></p>
我如何使用 hbs 来做同样的事情?我试过这个:
<div>
{{#each data}}
<div class="row">
<div class="col-md-12">
{{Title}}
</div>
</div>
{{/each}}
</div>
但它没有显示任何内容。
这是我的app.js:
const express = require("express"),
request = require("request"),
bodyParser = require("body-parser");
var app = express();
app.use(express.static("public"));
app.set("view engine", "hbs");
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/moviesearch", function(req, res){
res.render("moviesearch");
});
app.get("/movieResults", function (req, res) {
var user = req.query.getValue;
var url = "http://www.omdbapi.com/?s=" + user + "&apikey=...";
request(url, function (error, response, body) {
var data = JSON.parse(body);
res.render("API_Results", { data: data });
});
});
如何在 API_Result.hbs 上循环遍历这些值?
搜索示例:扫帚
{
Search: [
{
Title: "Bedknobs and Broomsticks",
Year: "1971",
imdbID: "tt0066817",
Type: "movie",
Poster: "https://m.media-amazon.com/images/M/MV5BMTUxMTY3MTE5OF5BMl5BanBnXkFtZTgwNTQ0ODgxMzE@._V1_SX300.jpg",
},
{
Title: "When Broomsticks Were King",
Year: "2001",
imdbID: "tt1418965",
Type: "movie",
Poster: "N/A",
},
],
totalResults: "5",
Response: "True",
}
谢谢
请查看以下代码。你必须像我们在 js 中那样 运行 一个循环。
<div>
{{#each data.Search}}
<div class="row">
<div class="col-md-12">
{{Title}}
</div>
</div>
{{/each}}
</div>