How to fix, "{ error: { status: 400, message: 'Bad search type field tracks' } }" occurred while using Spotify Search API

How to fix, "{ error: { status: 400, message: 'Bad search type field tracks' } }" occurred while using Spotify Search API

问题:解析时,终端中记录了以下错误。

{ error: { status: 400, message: 'Bad search type field tracks' } }

要关注的代码:

request({
    url: searchUrl,
    headers: {
      "Authorization": token
    }
  }, function(err, res) {
    if (res) {
      var data = JSON.parse(res.body);
      // var spotifyTrackIdAppJs00 = data.tracks.items[0].id;
      console.log(data);
      // console.log(trackId);
    }
)

完成app.js代码:

// It is a nodejs, expressjs project.
const express = require("express");

// 'body-parser' is used to access tags from the html file. We'll be using it to access queryValue.
const bodyParser = require("body-parser");

// request package; to fetch data from search endpoint.
const request = require("request");

// This app constant is created to be able to access the menthods available in 'express' package.
const app = express();

// 'urlencoded' helps access html data. Other data formats could JSON etc.
// body-parser required as to exclusively define "extended: true" although this is no use to us.
app.use(bodyParser.urlencoded({
  extended: true
}));

// This sets a static directory to look for source files like css, js, img. These file links are mentioned in html or ejs files.
// A folder named 'public' has to be in the same directory as "app.js". The source files are stored here.
app.use(express.static("public"));

// ejs view engine has been used to use app.js variables into the output ejs file.
app.set('view engine', 'ejs');

// Variable(s) to store the data fetched from API endpoint.
var data = "";

// The page to load when the browser (client) makes request to GET something from the server on "/", i.e., from the homepage.
// This GET request is made as soon as the homepage url is entered in the address bar od browser, automatically.
app.get("/", function(req, res) {
  res.sendFile(__dirname + "/index.html");
});

// The data that server should POST when the POST request is sent by the client, upon entering the search queryValue, in the search bar (form).
app.post("/", function(req, res) {

  // The user input query. We are using body-parser package here.
  const query = req.body.queryValue;

  // Follow procedure here to get access_token and refresh_token: https://benwiz.com/blog/create-spotify-refresh-token/
  const access_token = {access_token};
  const token = "Bearer " + access_token;
  var searchUrl = "https://api.spotify.com/v1/search?q=" + query + "&type=tracks&limit=4";

  request({
    url: searchUrl,
    headers: {
      "Authorization": token
    }
  }, function(err, res) {
    if (res) {
      var data = JSON.parse(res.body);
      // var spotifyTrackIdAppJs00 = data.tracks.items[0].id;
      console.log(data);
      // console.log(trackId);
    }

    // res.render("results", {
    //   spotifyTrackIdEjs00: spotifyTrackIdAppJs00
    // });
    // console.log("Value to be used in rendered file: " + spotifyTrackIdAppJs00);
  })
});

// Starting the server. Should this be placed at the top of all other commands?
app.listen(3000, function() {
  console.log("Server is running on port 3000.")
});

有用的资源:

我绝对是初学者。请像我五岁一样教我。非常感谢你。

你的代码看起来不错,但你有一个打字错误把一切都搞砸了。这个:

var searchUrl = "https://api.spotify.com/v1/search?q=" + query + "&type=tracks&limit=4";

Spotify's API 的参数 type 具有以下有效值: 专辑、艺术家、播放列表、曲目、节目和剧集。

在您的 searchUrl 上,您使用的是 tracks(复数)而不是 track,因此它应该是:

var searchUrl = "https://api.spotify.com/v1/search?q=" + query + "&type=track&limit=4";

对于未来:HTTP 的 400 代码表示“错误请求”,这意味着服务器告诉您请求中发送的内容有误。当您收到此类错误响应时,通常最好仔细检查您正在使用的 API 的必需 params/body 及其可接受的值