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
}
但它仍然返回:
TypeError: Cannot read property 'latitude' of undefined
好吧,你也可以检查代码是否是一个数组,并且至少有一个元素,比如:
if(Array.isArray(code(&;代码[0]({…做点什么。。。}