Node Postgre 参数化查询无法正常工作

问题描述 投票:0回答:1

我正在 Node Js & Express & TypeScrypt 中构建一个项目,当我尝试通过电子邮件检索用户的凭据时遇到一个奇怪的问题。查询无法查找或区分是否有任何有效凭据,仅始终返回空数组。当我检索整个集合并使用 Ts 对其进行过滤时,它会按预期工作。

这就是客户端进行查询的方式

async query(query: string , values : any[]) {
    if (!this._instance) {
      throw Error("PostGreClient instance is null");
    }
    if (!query) {
      throw Error("Query is empty");
    }
    // Log the query and the values to see what is being executed
    console.log("Executing query:", query);
    console.log("With parameters:", values);
    const obj = {
      text : query,
      values : values
    }
    return await this._instance.query(obj);
  }
async getCredentialsbyEmail(email: string): Promise<UserCredentials | null> {
      try{  
        this._debugListener("Retrieving credentials for email : " , email)
        const result = await this._client.query(`SELECT * from user_credentials WHERE email =$1` ,[email])
    
        const allCredentials = await this.getAllUserCredentials()
        const target = allCredentials.find((c)=> {
          return c.email === email
        })
      
        console.log("Existing Credentials : " +  JSON.stringify(allCredentials))
        this._debugListener("Current Result : " , result.rows)
        console.log("Expected Result = " , target)
        if(!result.rowCount || result.rowCount  === 0 ){
          return null
        }
        else if(result.rowCount >1){
          throw Error("Too many accounts linked to this email : " + email)
        }
        const credentials = result.rows[0].map((obj : any) => {
          return new UserCredentials(
            obj.uid,
            obj.userUid,
            obj.hashed_password,
            obj.email
          )
        })
        return credentials
      }catch(e){
        console.log(e)
        if(e instanceof Error){
          this._errorListener(e.message)
        }
        return null
      }
    }
javascript node.js typescript postgresql
1个回答
0
投票

在此输入链接描述

您可以在这里找到节点相关的帮助

© www.soinside.com 2019 - 2024. All rights reserved.