Typescript 在使用findById()和find()的单独方法上使用.populate()时会出现奇数差异

Typescript 在使用findById()和find()的单独方法上使用.populate()时会出现奇数差异,typescript,mongoose,Typescript,Mongoose,所以我读了我前辈的代码,我看到他做了这样的事情: const getTechnicianByIdQuery = await TechnicianModel.findById({id: someId}); const wantedTechnician = await this.populateSingleTechnician(getTechnicianByIdQuery); const populateSingleTechnician(technician: any): Promise<T

所以我读了我前辈的代码,我看到他做了这样的事情:

const getTechnicianByIdQuery = await TechnicianModel.findById({id: someId});
const wantedTechnician = await this.populateSingleTechnician(getTechnicianByIdQuery);

const populateSingleTechnician(technician: any): Promise<Technician>{
    const populatedTechnician = await technician
        .populate({path: 'salon',})
        .populate({path: 'user',})
        .execPopulate();
    return populatedTechnician;
};
const getTechnicianByIdQuery=wait TechnicianModel.findById({id:someId});
const wantedtechnology=wait this.populatesingletechnian(getTechnicianByIdQuery);
康斯特推广技术员(技术员:任何):承诺{
const populatedTechnician=等待技术人员
.populate({path:'salon',})
.populate({path:'user',})
.execPopulate();
归国技术人员;
};
这段代码运行得很好,我认为他的方法很酷,允许我们重用代码,所以我尝试使用find()而不是findById()进行不同的搜索:

const getTechnicianBySalonQuery=wait TechnicianModel.find({salon:someSalonId})
const wantedTechnicianGroup=等待此消息。populateTechnicianGroup(gettechnicianbysalquery);
const populateSingleTechnician(Technician Group:any):承诺{
const PopulatedTechnician=等待技术人员组
.populate({path:'salon',})
.populate({path:'user',})
.execPopulate();
归国技术人员;
};
我的代码给出错误:

{错误: [图形错误]{ 消息:“technicianGroup.populate不是函数”, 位置:[数组], 路径:[数组]}], 数据:null}

为什么我的代码会给出这个错误,而他的代码工作正常并返回填充的数据?如果我们说分离填充过程(通常作为链接操作符放入)导致了这个错误,我仍然想知道第一个代码块起作用而不是我的有什么区别


另一方面,我使用了传统的方法将填充()链接到查询,效果很好。

链接时,不应使用
wait
关键字。使用
wait
时,执行查询

看这张照片

const getTechnicianByIdQuery=TechnicianModel.findById({
id:someId,
});
const wantedtechnology=wait this.populatesingletechnian(getTechnicianByIdQuery);
康斯特推广技术员(技术员:任何):承诺{
const populatedTechnician=等待技术人员
.填充({
路径:“沙龙”,
})
.填充({
路径:“用户”,
})
.execPopulate();
归国技术人员;
};

第一个是有效的,因为
wait-findById
返回一个
mongoose.Document
,其中包含
.populate
。在第二种情况下,
mongoose.Document[]
没有
populate
方法(因为它是一个数组)



我看到您使用了typescript,我强烈建议您键入变量。您可能会在
技术人员处看到错误。如果
技术人员:任何
已被正确的类型替换,请填充

感谢您的快速响应,@Gregory!我担心的另一个问题是,如果没有
await
,那么
getTechnicianByIdQuery
是否会始终获取它应该按时获取的所有集合?第二个
wait
是否执行等待工作?调用
find()
将返回一个
Query
对象。此时,尚未与数据库进行任何交互。当您最终调用
exec()
(使用exec而不是execPopulate)时,mongoose将执行您描述的整个查询管道并获取所有数据。继续:是的,第二次等待会给你所有的答案
const getTechnicianBySalonQuery = await TechnicianModel.find({salon: someSalonId})
const wantedTechnicianGroup = await this.populateTechnicianGroup(getTechnicianBySalonQuery);

const populateSingleTechnician(technicianGroup: any): Promise<Technician[]>{
    const populatedTechnicians = await technicianGroup
        .populate({path: 'salon',})
        .populate({path: 'user',})
        .execPopulate();
    return populatedTechnicians;
};
const getTechnicianByIdQuery = TechnicianModel.findById({
  id: someId,
});

const wantedTechnician = await this.populateSingleTechnician(getTechnicianByIdQuery);

const populateSingleTechnician(technician: any): Promise<Technician> {
  const populatedTechnician = await technician
    .populate({
      path: 'salon',
    })
    .populate({
      path: 'user',
    })
    .execPopulate();
  return populatedTechnician;
};