ruby - 如何使用 Ruby 连接到 Firestore 数据库
问题描述
我正在尝试从 Ruby 脚本(最终从 Rails 应用程序)写入和读取 Firestore 数据库。
这似乎是一项微不足道的任务,但是当我在 IRB 中运行测试脚本时,它不会让我写入或读取数据。
我试过了:
- 成功安装
google-cloud-firestore
gem - 从 Firestore 控制台下载 JSON 密钥文件。
- 安全规则设置为公共(读写)
require 'google/cloud/firestore' # => true
# credentials
cred = Google::Cloud::Firestore::Credentials.new('<path/to/my/keyfile/key-projectname-etc.json>')
# => #<Google::Cloud::Firestore::Credentials:...>
# initialize firestore connection
db = Google::Cloud::Firestore.new(
project_id: '<MY-PROJECT-ID>',
credentials: cred
) # => #<Google::Cloud::Firestore::Client:...>
# write a document inside the users collection
db.doc("users/helloFromRuby").set({ name: "yay it worked!"})
这是我收到的错误消息:
Google::Cloud::UnavailableError (14:failed to connect to all addresses)
解决方案
我可以解决我的问题:我GOOGLE_APPLICATION_CREDENTIALS
初始化了一个环境变量,它以某种方式干扰了我的 json 密钥文件。删除该变量解决了我的问题。
推荐阅读
- javascript - 无需所有凭据护照即可登录
- python - 以非门作为预言的 Deutsch 算法
- java - 如何返回 android 应用程序并接受新的输入
- mysql - nginx:[emerg] /etc/nginx/sites-enabled/default:26 中上游“wordpress:/run/php/php7.3-fpm.sock”中的无效主机
- java - ArrayList 之间的区别
a = new ArrayList<>(); 并列出 a = new ArrayList<>(); - android - Android Studio 北极狐在 Macbook Air m1 上抛出 Aapt2InternalException
- admob - 如何修复不支持的语言 admob
- delphi - IOS部署错误[PAClient Error] E0776打包ipa文件时
- java - 如何将这个用双引号括起来的 JSON 字符串转换为对象
- swift - 使用变量时如何替换字符串的所有字符?