我正在使用与mongoDb连接的NestJ。 mongo中的对象(以及nestjs中的方案)具有以下结构{NAME: string, YEAR: number, COVER: string}
。
Estoy usando NestJs conectado con mongoDb。蒙古国的卫报》组织了
接口和dto具有结构
export class NAME {
readonly NAME: string;
readonly YEAR: number;
readonly COVER: string;
COVER_BASE64: string;
}
export interface NAME extends Document {
readonly NAME: string;
readonly YEAR: number;
readonly COVER: string;
COVER_BASE64: string;
}
在服务类中,我有下一个函数,它将附加到从Mongo获得的对象的cover_base64属性。
async getAlbums(): Promise<Album[]> {
return new Promise((resolve, reject) => {
this.model.find().exec((err, objs) => {
if (err) { reject(err); }
const objres = objs.map(obj => {
const base64 = this.getBase64(obj);
obj.COVER_BASE64 = base64;
return obj;
});
resolve(objsres);
});
});
}
问题是控制器类中返回的对象没有COVER_BASE64属性。但是在调试服务时,我看到每个“ albumsRes”对象都具有具有正确值的属性COVER_BASE64,但是调试控制器时该属性似乎已“消失”。
有一个原因是该属性不必出现在控制器中,例如在邮递员中,http请求没有获得COVER_BASE64属性。
答案 0 :(得分:0)
不是因为没有await
来获得新的承诺吗?它还说明了为什么在调试模式下它可以正常工作。
试试:
async getAlbums(): Promise<Album[]> {
return await new Promise((resolve, reject) => {
this.model.find().exec((err, objs) => {
if (err) { reject(err); }
const objres = objs.map(obj => {
const base64 = this.getBase64(obj);
obj.COVER_BASE64 = base64;
return obj;
});
resolve(objsres);
});
});
}