主檔案
const args = message.content.slice(prefix.length).trim().split(/ /);
const stringSimilarity = require("string-similarity");
const pokemons = require('../../arrays/pokemons.js')
const poss = stringSimilarity.findBestMatch(args, pokemons)
console.log(poss.bestMatch.target)
陣列檔案
exports.pokemons = [
example1,
example2,
example3,
etc...
]
我正在嘗試為用戶輸入的 pokemon 獲取最接近的名稱,但我收到此錯誤,Error: Bad arguments: First argument should be a string, second should be an array of strings而且我很確定我的引數是正確的
uj5u.com熱心網友回復:
這里有兩個問題。
第一個是您將陣列檔案作為物件匯出。這就是pokemons真正的情況,因為您將其設定為exports.pokemons:
{
pokemons: [
example1,
example2
]
}
你可以解構它:
const { pokemons } = require('../../arrays/pokemons.js')
第二個問題是您正在傳遞引數陣列。您可能只想要第一個引數。請記住,陣列是 0 索引的,并且您使其以命令開頭,因此您必須獲取1第一個引數的索引
const poss = stringSimilarity.findBestMatch(args[1], pokemons)
uj5u.com熱心網友回復:
正如錯誤所說,findBestMatch定義了第一個元素是 a string,但args肯定是陣列。findBestMatch再次檢查您的引數。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/359956.html
標籤:javascript 不和谐.js
下一篇:反應中自定義表單處理程式的問題
