【发布时间】:2020-04-30 19:23:17
【问题描述】:
我无法弄清楚这一点。我正在使用带有用打字稿编写的代码的节点快递猫鼬服务器。出现错误
“文档 | 类型”上不存在属性“填充”骨料 |型号 |查询'
对于我的 org.model.ts 文件中的以下代码:
OrgSchema.pre("findById", function(next) {
this.populate({
path: "acs",
select: "name -org"
}).populate({
path: "rcs",
select: "name -org"
});
next();
});
我错过了什么吗?因为相同的代码在 js 文件中工作。在打字稿中编写节点代码我还是新手。
【问题讨论】:
-
你能提供更多关于它的信息吗,你正在使用 findById 但是在哪里传递了 id 来过滤数据虽然我没有使用 typescript 但我想应该正确传递 id。
-
你可以使用 populate 作为 populate([{ path: "acs", select: { "name -org": 1 } }, { path: "rcs", select: { "name -org ": 1 } } ]) 希望这会有所帮助
-
id 作为路由参数传递。您的建议是在填充中使用对象数组。但我的问题是,在打字稿中,它根本不允许我在模态文件中使用填充。但我可以像这样直接在控制器文件中使用它:
-
导出异步函数 getOrg(req: any, res: any, next: any) { const org = await Organisation.findById(req.params.id).populate({ path: 'acs',选择:'name -org' }).populate({ 路径:'rcs',选择:'name -org' }); if (!org) { return next(new APIError(
No Org found with id ${req.params.id}, 404)); } res.status(200).json(org); } -
但是我想把它写成查询中间件
标签: javascript node.js mongodb typescript mongoose