使用subString在node.js中获取错误

时间:2019-03-04 01:45:41

标签: node.js

您好,这是我的第一个问题,所以我希望没事

我正在尝试获取我的代码以获取两个,,之间的内容,在本示例中,我需要获取prestons

Batehaven Cl,澳大利亚新南威尔士州普雷斯顿2170

我尝试过此代码

var country = getCountry(data.results[0]['address_components']);
var address = data.results[3]['formatted_address'];
var suburb = address;

suburb = suburb.subString(suburb.indexOf(",")+1, suburb.lastIndexOf(","));


  console.log(address);
  console.log(suburb);

但是我遇到一个错误,未处理的拒绝TypeError:郊区.subString不是函数

有人可以让我现在犯下什么愚蠢的错误

1 个答案:

答案 0 :(得分:0)

JavaScript方法区分大小写,请尝试 substring 以获取Cabramatta West郊区的两个词示例,即以下代码也可用于检索Prestons在您自己的示例中:

let address = "John St, Cabramatta West NSW 2166, Australia"
let suburb = address

suburb = suburb.substring(suburb.indexOf(',') + 1, suburb.lastIndexOf(','))

let states = ["TAS", "QLD", "VIC", "NSW", "SA", "WA"]
let positionOfState = -1
for (let i = 0; i < states.length; i++) {
  if (address.indexOf(states[i]) != -1) {
    positionOfState = suburb.indexOf(states[i])
    break;
  }
}
suburb = suburb.substring(1, positionOfState) // 1 is to skip the space at the start

console.log(`address -> ${address}`)
console.log(`suburb -> ${suburb}`)