问题描述
我正在使用bcrypt,但我正在尝试使用一个散列密码的异步函数并返回该密码,但是由于它是异步的,除非我使用同步版本(即使它可以工作,我也不希望这样做),否则它返回{{1 }},数据库将以下内容保存为: {} 。是的,有两个括号。我确实使用了[object Promise]
,但是它不起作用,并且我对异步函数的理解很差。我无法在线找到任何答案,因为按照教程的说明看起来我正在做的很好,但我显然没有。
代码如下:
await
那么我如何在不将异步数据库添加到异步代码的情况下返回散列密码呢?
解决方法
bcrypt.hash不返回承诺。
您可能需要将此函数调用包装成新的Promise
const hashedPass = await new Promise((resolve,reject) => {
bcrypt.hash(original_password,rounds,function(err,hash) {
if (err) reject(err)
resolve(hash)
});
,
当您调用getSprints(): Observable<Sprint[]> {
const token = localStorage.getItem('kno');
const rqwstrId = localStorage.getItem('rqwstrId');
let header = new HttpHeaders('Content-Type: application/json; charset=UTF-8');
header = header.append('kno',token);
header = header.append('rqwstrId',rqwstrId);
let param = new HttpParams().set('code',"Koon52");
return this.http.get<Sprint[]>(this.baseUrl + '/v1/sprints/',{headers : header,params : param});
}
函数时,它将返回一个export class Sprint {
kno:string;
rqwstrId : string;
code: string;
codeSprint : string;
dateDebut : string;
dateFin : string;
title : string;
constructor() {
}
}
,请检查此answer on SO。
为了获取散列数据,您可以使用SprintComponent.html:9 ERROR TypeError: Cannot read property 'codeSprint' of undefined
at Object.eval [as updateRenderer] (SprintComponent.html:9)
at Object.debugUpdateRenderer [as updateRenderer] (core.js:45294)
at checkAndUpdateView (core.js:44277)
at callViewAction (core.js:44637)
at execComponentViewsAction (core.js:44565)
at checkAndUpdateView (core.js:44278)
at callViewAction (core.js:44637)
at execEmbeddedViewsAction (core.js:44594)
at checkAndUpdateView (core.js:44272)
at callViewAction (core.js:44637)
来解决您的诺言
ngOnInit() : void {
this.sprintService.getSprints().subscribe(
data =>
{
this.sprints = data;
}
);
或者您也可以使用async
promise