Javascript 在mongodb中使用异步/等待预保存保存地理编码器坐标
我需要在mongodb中保存坐标,有人能帮我吗Javascript 在mongodb中使用异步/等待预保存保存地理编码器坐标,javascript,node.js,mongodb,Javascript,Node.js,Mongodb,我需要在mongodb中保存坐标,有人能帮我吗 UserScheme.pre('save', async function(next) { const doc = this const geo = await geocoder.geocode(this.address) this.lat = geo //here when I console log
UserScheme.pre('save', async function(next) {
const doc = this
const geo = await geocoder.geocode(this.address)
this.lat = geo
//here when I console log it shows undefined
console.log(this.lat)
//but here it shows the geocoder's response
console.log(geo)
})
可能是什么?有人能帮我吗
UserScheme.pre('save', async function(next) {
const doc = this
const geo = await geocoder.geocode(this.address)
this.lat = geo
//here when I console log it shows undefined
console.log(this.lat)
//but here it shows the geocoder's response
console.log(geo)
})
这是console.logs响应
undefined
[
{
latitude: -2.4454537,
longitude: -54.7186729,
formattedAddress: 'Avenida Jasmin, Santarém, Microrregião de Santarém,
Região Geográfica Intermediária de Santarém, Pará, Região Norte,
68020-280,
Brasil',
country: 'Brasil',
city: 'Santarém',
state: 'Pará',
zipcode: '68020-280',
streetName: 'Avenida Jasmin',
streetNumber: undefined,
countryCode: 'BR',
neighbourhood: '',
provider: 'openstreetmap'
},
]
好吧,我承认有一个非常愚蠢的方法来解决这个问题,我只是在UserScheme中创建了一个数组
const UserScheme = new mongoose.Schema({
coords:[
],
)}
然后:
UserScheme.pre('save', async function(next) {
const geo = await geocoder.geocode(this.address)
console.log(geo)
this.coords = geo
})
好的,很容易解决