如何使用Ruby连接到Firestore数据库 [英] How to connect to a Firestore database with Ruby
本文介绍了如何使用Ruby连接到Firestore数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图通过Ruby脚本(最终是从Rails应用程序)在Firestore数据库中读写数据.
I'm trying to write and read to and from my Firestore database from a Ruby script, eventually from a Rails application.
这似乎是一项微不足道的任务,但是当我在IRB中运行测试脚本时,它不会让我写入或读取数据.
It seems like a trivial task but when I run a test script inside IRB it won't let me write or read data.
我尝试过:
- 成功安装了
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!"})
这是我收到的错误消息:
This is the error message I get:
Google::Cloud::UnavailableError (14:failed to connect to all addresses)
推荐答案
我可以解决我的问题:我初始化了一个环境变量 GOOGLE_APPLICATION_CREDENTIALS
,它以某种方式干扰了我的json密钥文件.删除该变量解决了我的问题.
I could resolve my issue: I had an environmental variable GOOGLE_APPLICATION_CREDENTIALS
initialized that was somehow interfereing with my json keyfile. Deleting that variable solved my issue.
这篇关于如何使用Ruby连接到Firestore数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文