, country:data.city.country }, 预测:forecastList});
尝试{ const savedForecast = await newForecast.save(); return savedForecast.populate(‘location’)。 靠 </跨度> ().exec(); // FIXME: 靠 </跨度> ()这里抛出TypeError:savedForecast.populate(…)。 靠 </跨度> 不是一个功能} catch(err){ console.log(‘保存预测时出错。’+错误);}}
“newForecast”正在被保存
问题来自于这样一个事实 Document 不具有 lean() 方法。
Document
lean()
await newForecast.save(); 没有返回 Query 但是 Document 。然后跑 populate 在...上 Document 也回来了 Document 。转换 Document 到你必须使用的普通JS对象 Document.prototype.toObject() 方法:
await newForecast.save();
Query
populate
try { const savedForecast = await newForecast.save(); return savedForecast.populate('location').toObject(); // Wrong! `location` is not populated! } catch (err) { console.log('Error while saving forecast. ' + err); }
但是这段代码会执行错误 - 不会调用人口,因为 populate 必须收到回调参数或 execPopulate (它返回一个Promise)必须在它上面调用。就你所用而言 async/await 我建议使用 execPopulate 而不是回调。最后但并非最不重要的 - 人口稠密 location 需要倾斜:
execPopulate
async/await
location
try { const savedForecast = await newForecast.save(); return await savedForecast .populate({ path: 'location', options: { lean: true }}) .execPopulate() .then(populatedForecast => populatedForecast.toObject()); } catch (err) { console.log('Error while saving forecast. ' + err); }