- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 mongoose 新手,我正在尝试创建一个使用 OpenWeatherMap API 的应用程序。从 API 请求数据后,我将它们保存到我的 MongoDB 中,然后我想以 json 形式返回结果,因此我调用以下函数:
async function saveForecast(data) {
// Code here for creating the "forecastList" from the data and fetching the "savedLocation" from the DB
const newForecast = new Forecast({
_id: new mongoose.Types.ObjectId(),
location: savedLocation,
city: {
id: data.city.id,
name: data.city.name,
coordinates: data.city.coord,
country: data.city.country
},
forecasts: forecastList
});
try {
const savedForecast = await newForecast.save();
return savedForecast.populate('location').lean().exec(); //FIXME: The lean() here throws TypeError: savedForecast.populate(...).lean is not a function
} catch (err) {
console.log('Error while saving forecast. ' + err);
}
}
“newForecast”已成功保存在数据库中,但是当我在填充后尝试添加 .lean() 时,出现以下错误:类型错误:savedForecast.populate(...).lean 不是函数
我在查找查询上使用过lean(),它工作得很好,但我无法让它与我的“newForecast”对象一起工作,即使“savedForecast”是一个 Mongoose 文档,正如调试器向我显示的那样。
有什么想法为什么lean() 不起作用吗?谢谢!
最佳答案
问题来自于 Document
没有 lean()
方法。
await newForecast.save();
不会返回 Query
而是返回 Document
。然后在 Document
上运行 populate
也会返回 Document
。要将 Document
转换为普通 JS 对象,您必须使用 Document.prototype.toObject()方法:
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,因为populate
必须接收回调参数,或者必须调用execPopulate
(返回Promise)它。就您使用 async/await
而言,我建议使用 execPopulate
而不是回调。最后但并非最不重要的一点 - 需要了解填充的位置
:
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);
}
关于node.js - 对文档调用精益会抛出 TypeError : lean is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52950167/
我正在用 unity 3d 编写游戏,我在 3d 模型上映射 kinect 骨架。 现在我必须检查玩家是向左还是向右倾斜...这是我的问题。 我关注了这个网站:http://blogs.msdn.co
假设我想创建一个边界为 a b 的有界整数 Z。 def zbound (x₁ x₂ : ℤ) := { n : ℤ // x₁ ≤ n ∧ n ≤ x₂ } 这是有界整数的合理表示吗? 现在我想
我是初学者,但遇到以下问题: import tactic.linarith import tactic.suggest noncomputable theory open_locale classic
我证明了一些相当微不足道的引理 lemma two_ne_four_mul_any (n:ℕ) : 2 ≠ 2 * 2 * n 显然,这同样适用于非负整数、有理数、实数等: lemma two_ne_
我们正在使用 protobuf-net 库来解码 Inductive Automation - Ignition 软件提供的 MQTT 消息,该软件基于 sparkplug_b.proto 有效负载。
自从我开始学习交互式精益教程以来,一个问题一直困扰着我:Type 中单独的 Prop 层次结构的目的是什么? 据我现在的理解,我们有以下 Universe 层次结构: Type (n+1) |
我正在学习 Left-Lean-Red-Black 树,来自 Prof.Robert Sedgewick http://www.cs.princeton.edu/~rs/talks/LLRB/LLRB
Windows 提供了编码转换函数(“MultiByteToWideChar”和“WideCharToMultiByte”),它们能够进行 UTF-8 与 UTF-16 之间的转换,等等。但我看到人们
我是 mongoose 新手,我正在尝试创建一个使用 OpenWeatherMap API 的应用程序。从 API 请求数据后,我将它们保存到我的 MongoDB 中,然后我想以 json 形式返回结
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 5 年前。 Improve
我有两个 Mongoose 查询,并决定最好对它们使用 .lean()。 对于返回单个文档的,它似乎工作得很好: let something:Something; SomethingDocument.
我对 Promises 和 Bluebird 有点陌生。 我试图使用 Mongoose 和 Bluebird 进行查询。 我已经 promified mongoose 并将 Async 后缀添加到 f
我有一个只读 API,我希望 Mongoose 始终拥有 lean queries为了。 我能否在模式或连接级别启用此默认设置? 最佳答案 最简单的方法是猴子修补 mongoose.Query 类以添
我在 macOS Catalina 下使用美国键盘在 VS Code 中使用 Lean。如何输入符号,例如暗示箭头、联合、交集、子集? 是否有一些内置或附加调色板来促进这一点?或者我是否必须使用 Op
截至 DeprecationWarning 添加 here ,它似乎说 cursor() 是 stream() 的直接替代品,但是,有些功能似乎已被遗忘。 例如,这个已弃用的代码将包含不是 mongo
我有一个 Person 实体,它有自己的 Repository 类,我想测试它。这个存储库类按照 NestJS 文档中的建议注入(inject) Mongoose 模型,如下所示: @Inje
第一性原理的这种基本含义的证明,“精益定理证明”4.4 中的一个练习,击败了我迄今为止的所有尝试: open classical variables (α : Type) (p q : α → Pro
我使用美国海军公式修改了体脂计算器。计算器提供1.基础代谢率2. 最低热量要求3.体脂 计算器运行良好,但我希望基础代谢率利用瘦体重而不是体重(在表格中提供) 我补充说:在 BMR 计算之前,wt1
我正在使用lean ,(这使得查询更快,内存占用更少,但结果文档是普通的旧 JavaScript 对象(POJO),) 默认情况下,这将阻止所有 getter 执行并提供 POJO(普通旧 jav
当我发现像 WinForms 一样,Android 需要从主线程进行 UI 更新时,我一直在阅读 UI 更新(太糟糕了,我希望有人可以一劳永逸地解决这个恼人的问题) . 无论如何,我需要将其传递给 U
我是一名优秀的程序员,十分优秀!