![](/img/trans.png)
[英]TypeORM - How to access a related object inside another object in javascript
[英]How to access properties of a Promise<Object> in JavaScript (using TypeORM) for DB Querying
我正在使用 TypeORM 連接到 Postgres 數據庫,我對 JavaScript 和 TypeORM 等真的很陌生。所以我想做的事情應該不難,是在返回 object 屬性后訪問它來自 Postgres 的值。
因此,當我在 TypeORM 中使用 findOne() 方法時,它返回 Object 的 Promise 我返回,即在以下示例中,我想從數據庫返回單個用戶,以便我可以讀取其屬性並檢查其密碼是否匹配我搜索的用戶名支持這一點。
@Get(':username/:pwd')
authUser(@Param('username') username: string, @Param('pwd') pwd: string): Promise<User> {
const userToAuth = this.userAuthService.findOneUser(username);
return userToAuth;
}
我的問題是:有沒有辦法將 Promise 轉換為用戶 object 以便我可以訪問其屬性(在這種情況下為密碼),以便我可以進一步檢查密碼是否有效? 還是有辦法解決這個問題? 直接訪問承諾響應也是有效的,因為當我使用 postman 時,該方法返回一個 object,如下所示:
{
name: xxxx,
email: xxxx,
password: xxxx,
...
}
因此,如果我可以直接從方法中讀取這些屬性,那也很酷。
我希望一切都清楚,如果您需要有關該問題的更多信息,我將很樂意提供幫助::)
您可以將您的 function 轉換為async
並等待直到Promise<User>
解決:
@Get(':username/:pwd')
async authUser(@Param('username') username: string, @Param('pwd') pwd: string): Promise<User> {
const userToAuth = await this.userAuthService.findOneUser(username);
// Here you can run your validation logics, or further access user object internal properties
/* your-code-here */
// Finally you can return the result
return userToAuth;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.