1
00:00:00,491 --> 00:00:02,025
- [Instructor] Hi and welcome back.

2
00:00:02,025 --> 00:00:04,208
In this video, we're going
to be fixing the error

3
00:00:04,208 --> 00:00:06,583
that we found in the last video.

4
00:00:06,583 --> 00:00:08,282
In order to do that, we're going to need

5
00:00:08,282 --> 00:00:11,849
to be able to see what caused the error.

6
00:00:11,849 --> 00:00:14,775
So what to do is to download
the Heroku Command Line

7
00:00:14,775 --> 00:00:17,358
which gives us access to
a bunch of Heroku things,

8
00:00:17,358 --> 00:00:21,849
including the output of our
application in the console.

9
00:00:21,849 --> 00:00:24,548
That output, we can hope,
will have some information

10
00:00:24,548 --> 00:00:27,917
to help us troubleshoot this error.

11
00:00:27,917 --> 00:00:31,074
Also, if you can't
troubleshoot a specific error,

12
00:00:31,074 --> 00:00:33,183
it will also help me help you

13
00:00:33,183 --> 00:00:37,500
if you put the Heroku
logs out in the course Q&A

14
00:00:37,500 --> 00:00:39,633
if you have any problems.

15
00:00:39,633 --> 00:00:41,499
So to download the Heroku Command Line,

16
00:00:41,499 --> 00:00:42,932
just go to this URL,

17
00:00:42,932 --> 00:00:47,099
devcenter.heroku.com/articles/heroku-command-line.

18
00:00:49,632 --> 00:00:51,499
And then it's very simple for OS X.

19
00:00:51,499 --> 00:00:53,482
Just download and run the OS X installer,

20
00:00:53,482 --> 00:00:56,441
and for Windows, the Windows installer.

21
00:00:56,441 --> 00:00:59,292
Once you've done that, it's really simple,

22
00:00:59,292 --> 00:01:01,500
just go to your terminal.

23
00:01:01,500 --> 00:01:03,242
Make sure to quit your terminal,

24
00:01:03,242 --> 00:01:05,641
entirely exit the terminal,

25
00:01:05,641 --> 00:01:07,483
and then restart it again,

26
00:01:07,483 --> 00:01:11,049
so to reload all the changes
that Heroku has made.

27
00:01:11,049 --> 00:01:14,608
And then we're gonna do heroku login.

28
00:01:14,608 --> 00:01:16,533
And this is going to prompt you

29
00:01:16,533 --> 00:01:19,317
for your Heroku email
and your Heroku password,

30
00:01:19,317 --> 00:01:22,608
so type those in and then
you will be logged in

31
00:01:22,608 --> 00:01:26,200
to the Heroku Command Line interface.

32
00:01:26,200 --> 00:01:28,374
After that, once you've done that,

33
00:01:28,374 --> 00:01:30,041
do heroku logs --app

34
00:01:32,025 --> 00:01:34,142
and then we're going to
put in the name of our app

35
00:01:34,142 --> 00:01:37,975
which is stores-rest-api,
if I'm not mistaken.

36
00:01:41,308 --> 00:01:45,225
And here we have the
logs of our application.

37
00:01:45,225 --> 00:01:48,567
So as you can see, this stuff here

38
00:01:48,567 --> 00:01:51,817
was the starting up of the application.

39
00:01:53,033 --> 00:01:55,508
It says things, like, uWSGI is running

40
00:01:55,508 --> 00:01:57,017
in multiple interpreter mode,

41
00:01:57,017 --> 00:01:58,724
spawned master process,

42
00:01:58,724 --> 00:02:01,708
and then at the end,
it says, state changed

43
00:02:01,708 --> 00:02:04,049
from starting to up.

44
00:02:04,049 --> 00:02:06,782
That means that the
application's now running.

45
00:02:06,782 --> 00:02:09,033
Then we did our request

46
00:02:09,033 --> 00:02:10,817
for the path, a forward slash,

47
00:02:10,817 --> 00:02:12,641
we just access the home of the application

48
00:02:12,641 --> 00:02:14,483
and I told you that
this wasn't gonna work.

49
00:02:14,483 --> 00:02:17,208
This is not meant to be a
browser, blah, blah, blah.

50
00:02:17,208 --> 00:02:21,217
But really, the error
was something different

51
00:02:21,217 --> 00:02:23,716
as it seems from the logs.

52
00:02:23,716 --> 00:02:27,316
Apart from the fact that this path here

53
00:02:27,316 --> 00:02:30,100
doesn't return anything,

54
00:02:30,100 --> 00:02:33,717
that should have returned
an error code 404,

55
00:02:33,717 --> 00:02:35,167
which is Not Found,

56
00:02:35,167 --> 00:02:39,674
but really, what we saw
coming back is an error 500.

57
00:02:39,674 --> 00:02:41,724
500 means internal server error

58
00:02:41,724 --> 00:02:43,849
and it means that the
server couldn't tell us

59
00:02:43,849 --> 00:02:46,707
what was wrong, but something went wrong.

60
00:02:46,707 --> 00:02:50,874
So what we've got after that
is exactly what went wrong.

61
00:02:51,957 --> 00:02:53,383
Normally in these tracebacks,

62
00:02:53,383 --> 00:02:57,658
it starts at the point where
the error was returned to us,

63
00:02:57,658 --> 00:03:00,383
so it starts at the request dispatcher

64
00:03:00,383 --> 00:03:05,174
and then it starts at
try_trigger_before_first_request,

65
00:03:05,174 --> 00:03:07,283
and finally at the end here,

66
00:03:07,283 --> 00:03:09,740
it is our code, something
that looks familiar,

67
00:03:09,740 --> 00:03:11,700
db.create_all.

68
00:03:11,700 --> 00:03:14,249
And if you remember, we've
got this in our app.py

69
00:03:14,249 --> 00:03:17,157
and what that does is that
it goes to the SQLAlchemy

70
00:03:17,157 --> 00:03:20,366
and creates all of the tables,

71
00:03:20,366 --> 00:03:22,650
but there was an error, NameError,

72
00:03:22,650 --> 00:03:24,900
the name db is not defined.

73
00:03:26,241 --> 00:03:28,709
So that's interesting

74
00:03:28,709 --> 00:03:31,792
because, well, why is it not defined?

75
00:03:34,050 --> 00:03:37,692
We know that it's not
defined, so let's go into Atom

76
00:03:37,692 --> 00:03:39,400
and load up our app.py

77
00:03:39,400 --> 00:03:43,407
and see if we can identify
why it may not be defined.

78
00:03:43,407 --> 00:03:45,907
This is a really common error.

79
00:03:47,091 --> 00:03:50,582
Well, we've got all our imports up here.

80
00:03:50,582 --> 00:03:51,415
And then down here,

81
00:03:51,415 --> 00:03:54,782
we've got our
before_first_request decorator,

82
00:03:54,782 --> 00:03:56,483
which is the create_tables method,

83
00:03:56,483 --> 00:04:00,017
which is what runs db.create_all.

84
00:04:00,017 --> 00:04:03,600
But here's the thing,
where is db imported?

85
00:04:04,458 --> 00:04:07,041
'Cause it's certainly not here.

86
00:04:09,341 --> 00:04:12,625
And as you can see, db
is imported down here

87
00:04:12,625 --> 00:04:16,292
but db is imported
inside this if statement.

88
00:04:17,141 --> 00:04:19,599
Do you remember what
this if statement means

89
00:04:19,599 --> 00:04:22,099
or when this code is executed?

90
00:04:25,457 --> 00:04:26,857
I hope you did.

91
00:04:26,857 --> 00:04:30,690
This here runs when we
directly execute app.py

92
00:04:32,692 --> 00:04:34,667
from using the Python commands.

93
00:04:34,667 --> 00:04:36,500
If we do python app.py

94
00:04:38,082 --> 00:04:39,832
__name__ of the file,

95
00:04:41,750 --> 00:04:43,350
you use main.

96
00:04:43,350 --> 00:04:45,167
And therefore, this runs here.

97
00:04:45,167 --> 00:04:48,584
We import the db and then we run the app.

98
00:04:49,816 --> 00:04:53,941
However, when we run
the app through uWSGI,

99
00:04:53,941 --> 00:04:56,691
uWSGI is loading the app variable

100
00:04:58,366 --> 00:04:59,800
and running it itself.

101
00:04:59,800 --> 00:05:02,633
It is not running the app.py file.

102
00:05:04,240 --> 00:05:06,775
So therefore, this never runs.

103
00:05:06,775 --> 00:05:09,232
app.run doesn't run either

104
00:05:09,232 --> 00:05:12,166
but uWSGI runs the app for us.

105
00:05:12,166 --> 00:05:15,267
But the problem is that we're
not importing db anymore,

106
00:05:15,267 --> 00:05:17,308
so when it comes to
running this method here,

107
00:05:17,308 --> 00:05:20,550
it doesn't know that db exists.

108
00:05:20,550 --> 00:05:23,283
So how do we fix this problem?

109
00:05:23,283 --> 00:05:27,507
You may be tempted to
put this thing up here,

110
00:05:27,507 --> 00:05:30,525
but let me tell you, if you do
that, bad things will happen

111
00:05:30,525 --> 00:05:33,124
because of something
called circular imports.

112
00:05:33,124 --> 00:05:36,842
So if we put that up there,
we'll get a big mess.

113
00:05:36,842 --> 00:05:39,332
So what we have to do is create a new file

114
00:05:39,332 --> 00:05:42,417
which I'm gonna call run.py,

115
00:05:42,417 --> 00:05:43,742
although really it's
not gonna run anything,

116
00:05:43,742 --> 00:05:45,899
but nevertheless, run.py.

117
00:05:45,899 --> 00:05:50,066
And what run.py is gonna do
is it's going to import app

118
00:05:51,783 --> 00:05:54,116
and it's going to import db.

119
00:05:55,342 --> 00:05:57,582
Now, at the end, all that we have to do

120
00:05:57,582 --> 00:06:01,225
is say db.init_app(app).

121
00:06:01,225 --> 00:06:04,150
So essentially, it's doing
exactly the same thing

122
00:06:04,150 --> 00:06:05,933
that we're doing down here

123
00:06:05,933 --> 00:06:10,016
but it is not importing
the resources themselves.

124
00:06:11,732 --> 00:06:13,408
So hopefully that makes sense.

125
00:06:13,408 --> 00:06:16,225
And now, this run.py file

126
00:06:16,225 --> 00:06:19,225
also needs to contain the decorator,

127
00:06:21,624 --> 00:06:24,649
I'm gonna cut that and put it in here,

128
00:06:24,649 --> 00:06:27,308
so that when we run this file,

129
00:06:27,308 --> 00:06:29,941
this decorator does get created

130
00:06:29,941 --> 00:06:34,325
and it has access to
the db variable as well.

131
00:06:34,325 --> 00:06:36,717
So I'm gonna save both of these files

132
00:06:36,717 --> 00:06:38,317
and there we are.

133
00:06:38,317 --> 00:06:41,208
Now, we also have to make sure

134
00:06:41,208 --> 00:06:44,333
to change the uWSGI reference.

135
00:06:44,333 --> 00:06:48,733
We no longer want to
load up the app's app.

136
00:06:48,733 --> 00:06:51,233
Now we want to run run.py app,

137
00:06:52,858 --> 00:06:54,191
which is the same app,

138
00:06:54,191 --> 00:06:57,474
but if we load up the run.py decorator,

139
00:06:57,474 --> 00:07:01,249
that's going to execute this code here

140
00:07:01,249 --> 00:07:03,817
but it's gonna have
access to the db variable

141
00:07:03,817 --> 00:07:05,984
which it didn't in app.py.

142
00:07:06,874 --> 00:07:09,266
So just make sure in your uwsgi.ini

143
00:07:09,266 --> 00:07:12,183
to change the module to be run:app.

144
00:07:14,300 --> 00:07:16,574
And after that, let's
go back to our terminal

145
00:07:16,574 --> 00:07:18,132
and we're gonna clear this

146
00:07:18,132 --> 00:07:20,707
and we're gonna do git status

147
00:07:20,707 --> 00:07:23,982
and run.py and commit everything.

148
00:07:23,982 --> 00:07:27,650
In order to commit everything,
we can do git commit -a.

149
00:07:27,650 --> 00:07:30,957
So we're gonna do -am to
also type in a message

150
00:07:30,957 --> 00:07:34,624
and we're gonna say,
"Fixed not importing db

151
00:07:36,258 --> 00:07:38,175
"when running the app."

152
00:07:39,567 --> 00:07:41,734
And then we're gonna push.

153
00:07:43,849 --> 00:07:47,049
Once that's pushed,
we're gonna go to Chrome,

154
00:07:47,049 --> 00:07:50,433
which is where we've got our API

155
00:07:50,433 --> 00:07:53,716
and to make sure to go
ahead and press Deploy.

156
00:07:53,716 --> 00:07:58,582
So I've already deployed,
so I'm gonna deploy again,

157
00:07:58,582 --> 00:08:01,466
and that is it deploying.

158
00:08:01,466 --> 00:08:03,900
Now once that's deployed,
we're gonna go ahead

159
00:08:03,900 --> 00:08:07,342
and see if the output is any different.

160
00:08:07,342 --> 00:08:09,649
Notice that when we access the root,

161
00:08:09,649 --> 00:08:12,008
the homepage of our app,

162
00:08:12,008 --> 00:08:15,140
we should expect a 404 Not Found

163
00:08:15,140 --> 00:08:18,108
as opposed to a 500 Internal Server Error.

164
00:08:18,108 --> 00:08:19,916
404 Not Found is going to mean

165
00:08:19,916 --> 00:08:24,083
that there was no endpoints
defined for that address.

166
00:08:25,075 --> 00:08:28,749
500 Internal Server error means
that something went wrong.

167
00:08:28,749 --> 00:08:31,416
So let's go ahead and view this.

168
00:08:37,333 --> 00:08:38,292
And there we go.

169
00:08:38,292 --> 00:08:40,066
So now we get a 404 Not Found.

170
00:08:40,066 --> 00:08:42,725
That means there wasn't an error.

171
00:08:42,725 --> 00:08:45,258
It's just that we didn't define a path

172
00:08:45,258 --> 00:08:47,729
for the forward slash by itself.

173
00:08:47,729 --> 00:08:51,399
So what to do now is go over to Postman,

174
00:08:51,399 --> 00:08:53,982
change the URL to this URL here

175
00:08:55,917 --> 00:08:57,775
and try that everything works.

176
00:08:57,775 --> 00:08:59,657
So let's do that in the very next video

177
00:08:59,657 --> 00:09:01,020
and I'll see you there.

