How to connect to a Firestore database with Ruby

a 夏天 提交于 2021-02-10 20:25:29

问题


I'm trying to write and read to and from my Firestore database from a Ruby script, eventually from a Rails application.

It seems like a trivial task but when I run a test script inside IRB it won't let me write or read data.

I tried:

  • successfully installed google-cloud-firestore gem
  • downloaded JSON keyfile from within the Firestore console.
  • security rules are set to public (read and write)
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)

回答1:


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.



来源:https://stackoverflow.com/questions/57101015/how-to-connect-to-a-firestore-database-with-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!