首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Sendgrid实现-没有将nil隐式转换为String

Sendgrid实现-没有将nil隐式转换为String
EN

Stack Overflow用户
提问于 2018-04-17 10:58:42
回答 2查看 882关注 0票数 0

我把我的环境设置成这样:

代码语言:javascript
复制
echo "export SENDGRID_API_KEY='xxxxxxxxx'" > sendgrid.env
echo "sendgrid.env" >> .gitignore
source ./sendgrid.env

安装了Sendgrid。

我试图运行的代码:

代码语言:javascript
复制
require 'sendgrid-ruby'
include SendGrid
require 'json'

def hello_world
  from = Email.new(email: 'test@example.com')
  to = Email.new(email: 'test@example.com')
  subject = 'Sending with SendGrid is Fun'
  content = Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby')
  mail = Mail.new(from, subject, to, content)

  #previous version
  #sg = SendGrid::API.new(api_key: ENV['xxxxxxx'])

  #current version
  sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
  response = sg.client.mail._('send').post(request_body: mail.to_json)
  puts response.status_code
  puts response.body
  puts response.headers
end

hello_world

但我得到了以下错误:

代码语言:javascript
复制
no implicit conversion of nil into String

在此文件中:

代码语言:javascript
复制
/ruby/gems/2.4.0/gems/sendgrid-ruby-5.2.0/lib/sendgrid/client.rb:24:in

我不知道这里出了什么问题..。

完全错误:

代码语言:javascript
复制
/Users/pimzonneveld/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/sendgrid-ruby-5.2.0/lib/sendgrid/client.rb:24:in `+': no implicit conversion of nil into String (TypeError)
    from /Users/pimzonneveld/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/sendgrid-ruby-5.2.0/lib/sendgrid/client.rb:24:in `initialize'
    from app/helpers/mail.rb:12:in `new'
    from app/helpers/mail.rb:12:in `hello_world'
    from app/helpers/mail.rb:19:in `<main>'
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-04-17 11:16:48

您混淆了env变量(xxxxxxxxx)的值和它的名称(SENDGRID_API_KEY)。

在代码中设置API键时,请使用

代码语言:javascript
复制
  sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])

而不是。

票数 2
EN

Stack Overflow用户

发布于 2018-04-17 11:17:01

深是对的。/ruby/gems/2.4.0/gems/sendgrid-ruby-5.2.0/lib/sendgrid/client.rb:24:in指的是

代码语言:javascript
复制
"Authorization": "Bearer ' + @api_key + '",

因此,零到字符串的不隐式转换是完全合理的。

你应该换衣服

代码语言:javascript
复制
sg = SendGrid::API.new(api_key: ENV['xxxxxxx'])

适用于:

代码语言:javascript
复制
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49876315

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档