仍然尝试读取未定义的纬度,即使我声明了 typeof !== undefined
Still try read latitude of undefined even if i declared typeof !== undefined
UserScheme.pre('save', async function(next){
const code = await geocoder.geocode(this.address)
console.log(this.address)
console.log(code)
所以我前段时间遇到了这个麻烦所以我试着这样做:
if(typeof code !== "undefined"){
this.lat = code[0].latitude
this.lng = code[0].longitude
}
但它仍然 returns :
TypeError: Cannot read property 'latitude' of undefined
好吧,您还可以检查 code 是否是一个数组并且至少包含一个元素,例如:
if (Array.isArray(代码) && 代码[0]) {
... 做一点事 ...
}
UserScheme.pre('save', async function(next){
const code = await geocoder.geocode(this.address)
console.log(this.address)
console.log(code)
所以我前段时间遇到了这个麻烦所以我试着这样做:
if(typeof code !== "undefined"){
this.lat = code[0].latitude
this.lng = code[0].longitude
}
但它仍然 returns :
TypeError: Cannot read property 'latitude' of undefined
好吧,您还可以检查 code 是否是一个数组并且至少包含一个元素,例如:
if (Array.isArray(代码) && 代码[0]) { ... 做一点事 ... }