【问题标题】:Cannot Grab Data From Primary Key In RethinkDB无法从 RethinkDB 的主键中获取数据
【发布时间】:2019-02-11 03:31:43
【问题描述】:

我正在尝试通过主键获取 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.

【问题讨论】:

    标签: rethinkdb rethinkdb-javascript rethinkdbdash


    【解决方案1】:

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

    【讨论】:

    • 接受此答案为正确答案,以便其他人发现它有用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多