gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 08:45:14 26 4
gpt4 key购买 nike

我一直在尝试将process.argv [3]拆分为多个单词时输入,但是由于某种原因,到目前为止,我尝试过的所有操作均产生了以下错误:在搜索spotify-this-song/do-what-it时-不带参数的说法,而不是搜索默认值,而是显示“您必须指定类型并查询搜索内容”。另一个问题是,我尝试将电影查询放入每次搜索的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个参数。

关于javascript - 如何在node.js上拆分参数而不为默认搜索创建错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56299960/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com