rethinkdb - 无法从 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.
解决方案
我发现了这个问题,primarykey 需要一个整数而不是字符串,因此它会查找整数而不是字符串,并且由于找不到它,因此会引发错误。要修复它,我只需要将数据从整数更改为字符串。您也可以只使用 parseInt 函数将数组从字符串转换为整数。
推荐阅读
- aws-api-gateway - 自定义域不适用于无服务器应用程序
- c++ - 为什么 float、double 和 long double 给出相同的输出?
- javascript - 使用 service worker 将链接图像保存在缓存内的应用程序中或使用 nedb 保存在数据库中
- python - 有没有办法可以将此错误放入python中的变量中?
- image - 我的图片没有上传到我的 html 网站,使用了 atom 代码编辑器应用程序,
- forms - 如何访问来自表单 blob 的每一行 textarea?
- php - 简码不返回两个相同的属性
- javascript - React js - this.state 中的 API 响应是空对象
- ruby-on-rails - 如何使用 NPM、Webpacker 和 Rails 安装 Quill dev 2.0?
- android - Android Studio:缺少 Android 支持存储库