通过postgres pg包更新数据的问题



我有一个用angular和node.js构建的应用程序(带有pg npm包,版本= 8.7.1)该应用分为微服务。每个服务器应用程序都有"pg"软件包已安装并连接到postgres db。问题是,如果我运行一些&;update&;查询,之后我运行getList查询,然后我得到旧的值,而不是更新的对象。如果我添加setTimeout为5秒那么它就可以正常工作了

在我的本地主机上都工作正常。这个问题只发生在服务器上的heroku (postgres在云端)上。有时我得到更新的数据,有时没有

下面是我的代码:

客户端代码(angular) -调用更新函数c,然后使用async &等待

async filter({ value }) {
const list: any = await this.getList() 
const [myData]: any = await this.updateData(this.value) 
const list: any = await this.getList()  // Here is the issue !!
}

函数像这样调用到服务器的API:

getList(): Promise<any> {
return this.http.get<any>(`${ENV.BASE_API}/doGetApiCalls`).toPromise();
}
updateData(value: any): Promise<any> {
return this.http.put<any>(`${ENV.BASE_API}/doUpdateApiCalls`, value).toPromise();
}

服务器代码为:提单代码

async function updateData(description, id) {
let query = updateDataQuery(description, id);
let results = await postgressQuery(query);
return getDataResults;
}

DEL代码

function updateDataQuery(description: string, id:number) {
const query = `UPDATE public.books
SET description='${description}', 
WHERE book =${id}
RETURNING *`
return query;
}

这里是连接到postgres db (BL调用lib通过import this)

const DATABASE_URL = process.env.DATABASE_URL;
const pool = new Pool({
connectionString:DATABASE_URL,
ssl:{rejectUnauthorized: false}
})

let openConnect = async () => {
await pool.connect();
}
let postgressQuery = async (q) => {
try {
const result = await pool.query(q);
return await result.rows;
}
catch (e) {
console.log(e);
}
}

========================================================

如果我在客户端添加await,那么它工作得很好。更新需要一段时间?

async filter({ value }) {
const list: any = await this.getList()  //
const [myData]: any = await this.updateData(this.value) //get the RETURN from server with correct data
await new Promise(resolve => setTimeout(resolve, 5000)) //added for wait for 5 sec
const list: any = await this.getList() // then data is correct (aafer 5 sec)
}

上面的代码有什么问题?提前感谢

我找到了一个解决方案:

缓存问题。只需要安装这个包,一切都很好!https://www.npmjs.com/package/nocache

最新更新