node.js - Node.js客户端的Aerospike同步获取方法

标签 node.js aerospike

Aerospike for Node.js 客户端有同步数据库读写的方法吗?

最佳答案

Aerospike 客户端中的所有 API 调用都使用异步。连接、读取数据等的模式,这是 Node.js 中所期望的。

不过,您可以使用新的(自 Node 7.6 起)async/await 关键字来允许以更具可读性的方式编写代码。它不是同步的(因为 testRead 不会阻塞),但它读取起来更像同步代码。

const Aerospike = require('aerospike')
var batchRecords = [
  { key: new Aerospike.Key('test', 'demo', 'key1'), bins: ['i', 's'] },
  { key: new Aerospike.Key('test', 'demo', 'key2'), read_all_bins: true },
  { key: new Aerospike.Key('test', 'demo', 'key3') }
];

async function testRead()
{
    var result = await readFromAerospike(batchRecords);
    console.log(result);
}

function readFromAerospike(batchRecords) {
   return new Promise((resolve, reject) => {
       Aerospike.connect((err, client) => {
          if (err) {
              reject(err);
          } else {
              client.batchRead(batchRecords, (err, results) => {
                  if (err) {
                      reject(err);
                  } else {
                      resolve(results);
                  }
              })
          }
       });
   });
}

testRead();

关于node.js - Node.js客户端的Aerospike同步获取方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49848043/

相关文章:

javascript - NestJS:如何在自定义验证器中同时访问 Body 和 Param?

node.js - 我的express.js hello世界和Docker安装程序在做什么?

java - Spring MVC - 实现 aerospike session

python - CAS 对 aerospike python 客户端的支持

node.js - Socket.io 延迟问题

node.js - 如何在 WebStorm 中使用 TypeScript 调试用 Node.js 编写的后端

javascript - 使用 Mongoose/MongoDB 创建一个独立的用户模式并将其嵌入到消息模式中?

aerospike - AQL 无法通过 PK 找到行。选择时有行

c# - 如何解决 AerospikeClient 错误代码 1