you know that the result await User.find({zipcode: { $exists: true }})
is a Array of Model, so in map
, each user is a model...
for your problem don't need to use findByIdAndUpdate
you can try this
try {
const userWithObjectLocation = await User.find({zipcode: { $exists: true }});
userWithObjectLocation.map(async user =>{
const locationObject = zipcodes.lookup(user.zipcode);
if(locationObject){
const location = `${locationObject.city}, ${locationObject.state}`
user.location = location;
await user.save()
}
})
return res.status(200).json({message: "Location has been updated successfully"});
} catch (err) {
console.log(err);
return res.status(500).json(err);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…