Thomas Allen Thomas Allen - 1 year ago 74
Ruby Question

TypeError (no implicit conversion of nil into String) on login when deployed to Heroku

I deployed a project to Heroku and am currently getting a "TypeError (no implicit conversion of nil into String)" in my heroku logs when I try and login.

It works fine locally so I am unsure as to what's going wrong.

2017-05-22T09:28:53.701881+00:00 app[web.1]: I, [2017-05-22T09:28:53.701785 #4] INFO -- : [9748b196-6755-447a-a783-
03e1d4636790] Completed 500 Internal Server Error in 76ms (ActiveRecord: 1.0ms)
2017-05-22T09:28:53.624924+00:00 app[web.1]: I, [2017-05-22T09:28:53.624813 #4] INFO -- : [9748b196-6755-447a-a783-03e1d4636790] Started POST "/login" for 81.154.127.158 at 2017-05-22 09:28:53 +0000
2017-05-22T09:28:53.702530+00:00 app[web.1]: F, [2017-05-22T09:28:53.702491 #4] FATAL -- : [9748b196-6755-447a-a783-03e1d4636790]
2017-05-22T09:28:53.702586+00:00 app[web.1]: F, [2017-05-22T09:28:53.702545 #4] FATAL -- : [9748b196-6755-447a-a783-03e1d4636790] lib/auth.rb:8:in `issue'
2017-05-22T09:28:53.702441+00:00 app[web.1]: F, [2017-05-22T09:28:53.702368 #4] FATAL -- : [9748b196-6755-447a-a783-03e1d4636790]
2017-05-22T09:28:53.702489+00:00 app[web.1]: F, [2017-05-22T09:28:53.702444 #4] FATAL -- : [9748b196-6755-447a-a783-03e1d4636790] TypeError (no implicit conversion of nil into String):
2017-05-22T09:28:53.702587+00:00 app[web.1]: [9748b196-6755-447a-a783-03e1d4636790] app/controllers/authentications_controller.rb:16:in `login'


I think it's saying something is going wrong in either my auth.rb or my auth controller but can't see where.

lib/auth.rb:

def self.issue(payload, expiry_in_minutes=60*24*30)
payload[:exp] = expiry_in_minutes.minutes.from_now.to_i
JWT.encode(payload, auth_secret, ALGORITHM)
end


authentications_controller.rb:

def login
user = User.find_by_email(params[:email])
if user && user.authenticate(params[:password])
token = Auth.issue({ id: user.id })
render json: { token: token, user: UserSerializer.new(user) }, status: :ok
else
render json: { errors: ["Invalid login credentials."] }, status: 401
end
end


You can have a look at the full repo HERE if needed.

Any help would be massively appreciated!

Answer Source

It turned out (from the comments), that there was a missing environment variable. Setting those in heroku is done like this from the terminal/console.

heroku config:set MY_VARIABLE=myvalue

Getting the list of environment variables is equally enjoyable with

heroku config
# MY_VARIABLE:   myvalue 

https://devcenter.heroku.com/articles/config-vars

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download