【发布时间】:2021-12-05 08:30:39
【问题描述】:
我正在尝试返回图标,结果是在页面上生成它们
下面map下面的代码是我试图返回页面的所有结果。
const icons = await Icon.find({ _id: { $in: ids } }).select({ ...options.select, ...options.additional })
const results = ids
.map(id => {
const iconsGroup = icons.find(icon => icon.id === id)
if (query.hasPublishingError) {
iconsGroup.icons = iconsGroup.icons.filter(el => (el.errorsData.length > 0 && el.status === 'draft'))
}
return iconsGroup
})
.map(i => i.toJsonWith(options.select))
await postProcess(results)
return {
docs: results,
limit: response.limit,
page: response.page,
pages: response.pages,
total: response.total
}
当我发出GET 请求时,我收到如下错误:
TypeError: Cannot read property 'toJsonWith' of undefined\n
痛点是.map(i => i.toJsonWith(options.select))
顺便说一句,由于某种原因,这个函数toJsonWith 无法在某处作为声明找到。
请帮帮我,我试图弄清楚这里出了什么问题 4 天。
【问题讨论】:
-
cannot read property 'toJsonWith' of undefined 意味着在这种情况下
i是未定义的,所以可能在映射之前检查结果是否为空 -
函数
toJsonWith是你自己写的吗?如果不是你可能忘记导入它,但我没有找到任何包含这个功能的包 -
@mrJQuery linter 告诉我可以从某个对象调用
toJsonWith(),我认为不需要导入它。是的,i很可能是未定义的,但我不明白如何重新评估此表达式以使其正常工作,同样如果i未定义,为什么字段id是 not undefined -
i未定义,因为您的第一张地图不返回任何内容。 if 会检查icons.find(icon => icon.id === id)是否返回任何东西。
标签: javascript node.js typescript dictionary nestjs