简体   繁体   English

使用 AXIOS 进行电影搜索后,OMDb API 需要帮助获取 /results 页面

[英]OMDb API need help getting /results page after movie search using AXIOS

this is my current app.js code.这是我当前的 app.js 代码。 I am trying to create a movie search that allows me to search a movie within a database and bring me back results of 10 movies within that keyword.我正在尝试创建一个电影搜索,它允许我在数据库中搜索电影,并带回该关键字内 10 部电影的结果。 For example.例如。 If I search "ALABAMA", I should receive back 10 movies in the database with the words ALABAMA.如果我搜索“ALABAMA”,我应该会在数据库中收到带有 ALABAMA 字样的 10 部电影。 However, I cannot move forward because I am stuck on trying to use node.js, express.js, and axios together.但是,我无法继续前进,因为我一直在尝试同时使用 node.js、express.js 和 axios。 Previously I was using Request instead of Axios, but because it has been deprecated, I opted to use axios instead.以前我使用 Request 而不是 Axios,但因为它已被弃用,我选择使用 axios。 I am still learning, as it a bit tricky, but I just need help trying to get my /results.我仍在学习,因为它有点棘手,但我只需要帮助尝试获得我的 /results。 page to load.要加载的页面。 My VIEWS folder contains a "results.ejs" and "search.ejs".我的 VIEWS 文件夹包含“results.ejs”和“search.ejs”。 I am using the OMDb API from http://omdbapi.com/ .我正在使用来自http://omdbapi.com/的 OMDb API。 Any feedback will be greatly appreciated, I'm just trying to know where I messed up.任何反馈将不胜感激,我只是想知道我在哪里搞砸了。

const express = require("express");
    const app = express();
    const axios = require("axios"); 
     
    app.set("view engine", "ejs");
    app.use(express.static(process.env.STATIC_DIR || "public"));
     
    app.get("/", function(req, res){
        res.render("search.ejs")
    })
    
    app.get("/results", (req, res) => {
        var searchTerm = req.query.search;
        var url = "https://www.omdbapi.com/?s=" + searchTerm + "&apikey=thewdb";
        axios.get('http://www.omdbapi.com/?i=tt3896198&apikey=thewdb')
        
        .then(response => {
            console.log(response.data.Search);
        res.render("results.ejs", {data: response.data.Search});
        })
        .catch(err => {
            console.log(err);
        })}) 
     
    app.listen(3000, () => {
        console.log("Movie App Has Started!!");
    })

my results.ejs code is below:我的 results.ejs 代码如下:

<h1>Results Page!!!</h1>
 
<% if (data == null) { %>
<p>No results. Please try refining your search terms.</p>
<% } else { %>
 
<ul>
    <% data["Search"].forEach(function(movie) { %>
    <li>
        <strong>
            <%= movie["Title"] %>
        </strong>
        - <%=movie["Year"]%>
    </li>
    <% }) %>
</ul>
 
<% } %>
 
<a href="/">Search Again!</a>

my search.ejs code is below:我的 search.ejs 代码如下:

<h1>
    Search For a Movie
</h1>

<form action="/results" method="GET">
    <input type="text" placeholder="search term" name="search">
    <input type="submit">
</form>

在此处输入图片说明

It seems you are passing a hardcoded URL string instead of the prepared URL string with the Search Term.您似乎正在传递一个硬编码的 URL 字符串,而不是带有搜索词的准备好的 URL 字符串。

Change this改变这个

 var url = "https://www.omdbapi.com/?s=" + searchTerm + 
 "&apikey=thewdb";
axios.get('http://www.omdbapi.com/?i=tt3896198&apikey=thewdb')

To this对此

var url = "https://www.omdbapi.com/?s=" + searchTerm +"&apikey=thewdb";

axios.get(url)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM