繁体   English   中英

如何在node.js上拆分参数,而不会为默认搜索创建错误

[英]How can I split my parameter on node.js without creating an error for a default search

我一直在尝试拆分process.argv [3],因为当输入是多个单词时,但由于某种原因,到目前为止我所尝试的一切都创建了这些错误:当搜索spotify时 - 这首歌/做什么它-says没有参数,而不是搜索默认值,它说“你必须为搜索指定一个类型和查询。” 另一个问题是我尝试将我的电影查询放在每个搜索的for循环中,以将我的默认搜索与queryURL分开,而不是搜索我输入的参数,它什么也没显示。 是不是可以这样做,还是我只是遇到语法问题?

require("dotenv").config();
var fs = require("fs");
var keys = require("./keys.js");
var Spotify = require("node-spotify-api");
var axios = require("axios");
var bandsintown = require("bandsintown")(keys.spotify.bandsintown);
//console.log(keys.spotify.bandsintown);
var spotify = new Spotify(keys.spotify);
var moment = require("moment");
moment().format();
//arguments
var p = process.argv;
var arg = p[2];
var v = p[3];

//execute
input(arg, v);

//funk
function input(arg, v) {
  switch (arg) {
    case "spotify-this-song":
      songQuery(v);
      break;
    case "movie-this":
      movieQuery(v);
      break;
    case "concert-this":
      bandQuery(v);
      break;
    case "do-what-it-says":
      makeMe();
      break;
    default:
      console.log(
        "Input one of the following functions with a paramter: \nspotify-this-song \nmovie-this \nconcer-this \ndo-what-it-says"
      );
  }
}

//Spotify Funk
function songQuery(v) {
  // let a = p.splice([3]).join("+");

  //default
  if (v === undefined) {
    v = "The Sign";
  }
  spotify
    .search({ type: "track", query: v, limit: 10 })
    .then(function(response) {
      //console.log(response.tracks);
      let song = response.tracks.items;
      for (i = 0; i < song.length; i++) {
        fs.appendFileSync("log.txt", "NEW QUERY\n");
        console.log(i);
        fs.appendFileSync("log.txt", i + "\n");
        console.log(song[i].artists[0].name);
        fs.appendFileSync(
          "log.txt",
          "artist: " + song[i].artists[0].name + "\n"
        );
        console.log(song[i].name);
        fs.appendFileSync("log.txt", "song: " + song[i].preview_url + "\n");
        console.log(song[i].preview_url);
        fs.appendFileSync(
          "log.txt",
          "preview url: " + song[i].preview_url + "\n"
        );
        console.log(song[i].album.name);
        fs.appendFileSync("log.txt", "album: " + song[i].album.name + "\n");
      }
    })
    .catch(function(err) {
      console.log(err);
    });
}

function movieQuery(v) {
  //default
  if (v === undefined) {
    v = "Mr. Nobody";
    console.log(
      "If you haven't watched 'Mr. Nobody,' then you should: http://www.imdb.com/title/tt0485947/"
    );
    fs.appendFileSync(
      "log.txt",
      "If you haven't watched 'Mr. Nobody,' then you should: http://www.imdb.com/title/tt0485947/" +
        "\n"
    );
    console.log("It's on Netflix!");
    fs.appendFileSync("log.txt", "It's on Netflix!\n");
  }
  //split everytime there is an empty space
  var dataArr = p.splice([3]).join("+");
  // Then run a request with axios to the OMDB API with the movie specified
  var queryUrl = `http://www.omdbapi.com/?t=${dataArr}&y=&plot=short&apikey=trilogy`;
  // helps debug against actual URL.
  //console.log(queryUrl);
  axios
    .get(queryUrl)
    .then(function(response) {
      let mData = response.data;
      //console.log(mData);
      // for (i = 0; i < mData.length; i++) {
      console.log("Movie Title: " + mData.Title);
      console.log("Release Year: " + mData.Released);
      console.log("IMBD Rating: " + mData.imdbRating);
      console.log("Rotten Tomatoes Rating: " + mData.Ratings[1].Value);
      console.log("Country Produced: " + mData.Country);
      console.log("Language: " + mData.Language);
      console.log("Plot: " + mData.Plot);
      console.log("Actors: " + mData.Actors);
      fs.appendFileSync("log.txt", "Movie Title: " + mData.Title + "\n");
      fs.appendFileSync("log.txt", "Release Year: " + mData.Released + "\n");
      fs.appendFileSync("log.txt", "IMBD Rating: " + mData.imdbRating + "\n");
      fs.appendFileSync(
        "log.txt",
        "Rotten Tomatoes Rating: " + mData.Ratings[1].Value + "\n"
      );
      fs.appendFileSync("log.txt", "Country Produced: " + mData.Country + "\n");
      fs.appendFileSync("log.txt", "Language: " + mData.Language) + "\n";
      fs.appendFileSync("log.txt", "Plot: " + mData.Plot + "\n");
      fs.appendFileSync("log.txt", "Actors: " + mData.Actors + "\n");
      //}
    })
    // Then log the Release Year for the movie

    .catch(function(error) {
      if (error.response) {
        // The request was made and the server responded with a status code
        // that falls out of the range of 2xx
        console.log("---------------Data---------------");
        console.log(error.response.data);
        console.log("---------------Status---------------");
        console.log(error.response.status);
        console.log("---------------Status---------------");
        console.log(error.response.headers);
      } else if (error.request) {
        // The request was made but no response was received
        // `error.request` is an object that comes back with details pertaining to the error that occurred.
        console.log(error.request);
      } else {
        // Something happened in setting up the request that triggered an Error
        console.log("Error", error.message);
      }
      console.log(error.config);
    });
}

function bandQuery(v) {
  let dataArr = v.split(" ").join("+");
  let queryUrl = `https://rest.bandsintown.com/artists/${dataArr}/events?app_id=${
    keys.spotify.bandsintown
  }`;
  //console.log(bandsintown);
  axios.get(queryUrl).then(function(response) {
    //console.log(response.data);
    let fu = response.data[0];
    console.log("Venue: " + fu.venue.name);
    fs.appendFileSync("log.txt", "Venue: " + fu.venue.name + "\n");
    let city = fu.venue.city;
    let region = fu.venue.region;
    let country = fu.venue.country;
    console.log(`Location: ${city}, ${region}, ${country}`);
    fs.appendFileSync("log.txt", `Location: ${city}, ${region}, ${country}\n`);
    console.log(moment(fu.datetime).format("MM/DD/YYYY"));
    let thyme = moment(fu.datetime).format("MM/DD/YYYY");
    fs.appendFileSync("log.txt", `Date: ${thyme}\n`);

  });
}

function makeMe() {
  fs.readFile("random.txt", "utf8", (err, data) => {
    if (err) throw err;
    console.log(data);
    var dataArr = data.split(",");
    input(dataArr[0], dataArr[1]);
  });
}

做法:

使用一点es6解构。

在控制台中运行: node app.js a 5 3 ;

const [a, b, c, d, e, f] = process.argv;

console.log(c);
console.log(d);
console.log(e);
console.log(f);

f应该是未定义的,c / d / e应该全部定义为你传入的3个参数。

暂无
暂无

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

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