首页 > 解决方案 > 无法从 RethinkDB 中的主键中获取数据

问题描述

我正在尝试通过主键获取 RethinkDB 中的数据。但是,我的代码找不到数据。

我正在使用 RethinkDBDash 驱动程序来获取数据等,我将 Rethink 定义为 client.db。ID 是我从 API 中获取的数据的切片数组,当我在控制台记录它时,值为 1。我通过简单地输入整数 1 进行了测试,它能够获取数据,但是,当我使用 API 中的数据抓取它,它返回一个空错误。我试图获取数据的表名为建议,主键为 sid,最初为 id。该表如下所示:

{
"author": {
"id":  "535986058991501323"
} ,
"sid": 1 ,
"status":  "PENDING" ,
"suggestion":  "wtf"
}

在过去的项目中,我已经能够使用这种精确的方法从主键中获取数据。

const id = args.slice(0).join(' ');
console.log(id);
const data = await client.db.table('suggestions').get(id).run();
console.log(data.sid);

通过抓取数据,我期望控制台日志的值为 1。但是,我得到的是一个错误,它说明了这一点:

(node:34984) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'sid' of null
    at Object.run (C:\Users\facto\Desktop\Projects\JavaScript\Bots\CountSill\commands\deny.js:7:26)
(node:34984) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:34984) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

标签: rethinkdbrethinkdb-javascriptrethinkdbdash

解决方案


我发现了这个问题,primarykey 需要一个整数而不是字符串,因此它会查找整数而不是字符串,并且由于找不到它,因此会引发错误。要修复它,我只需要将数据从整数更改为字符串。您也可以只使用 parseInt 函数将数组从字符串转换为整数。


推荐阅读