在node.js中,我有一个databaseMapper.js
文件,该文件使用Ojai节点MapR api。提取数据。到目前为止,我可以处理单个文档,但是由于这是一个异步api,因此在查询多个文档时会遇到一些问题。
这是我到目前为止所拥有的:
function queryResultPromise(queryResult) {
//this should handle multiple promises
return new Promise((resolve, reject) => {
queryResult.on("data", resolve);
// ...presumably something here to hook an error event and call `reject`...
});
}
const getAllWithCondition = async (connectionString, tablename, condition) =>{
const connection = await ConnectionManager.getConnection(connectionString);
try {
const newStore = await connection.getStore(tablename);
const queryResult = await newStore.find(condition);
return await queryResultPromise(queryResult);
} finally {
connection.close();
}
}
这里它将仅返回第一个,因为queryResultPromise
将在第一个文档上显示resolve
。但是,在查询结果将像"data"
这样结束之前,queryResult.on('end', () => connection.close())
的回调可能会发生多次。
我尝试使用类似Promise.all()
的方法来解决所有问题,但我不确定如何将queryResult.on
回调包含在此逻辑中
在node.js中,我有一个databaseMapper.js文件,该文件使用Ojai节点MapR api。提取数据。到目前为止,我可以使用单个文档,但是由于这是一个异步api,因此我有一些问题...
这将起作用