我正在制作一个以Create-Repack-App --full开头的ReactJs应用程序,它为我设置了很多东西,包括使用Devise gem的用户,并且无需一路设置标题。我使用典型的test@test.com等创建了一个用户,但是在尝试加载主页时出现标题错误。我试图让我的主页根据用户是否登录而有条件地呈现。
以下是组件:
import React, { Component } from 'react';
import { Header } from 'semantic-ui-react';
class Home extends Component {
homePage = () => {
debugger
const { user } = this.props;
if (user.id) {
return (
<div>
<div>Games</div>
<div>Sessions</div>
<div>Friends</div>
<div>Tools</div>
<div>Help</div>
<div>Settings</div>
<div>Profile</div>
</div>
);
}
return (
<div>
<Header as='h1' textAlign='center'>Welcome to Board Game Tracker</Header>
<Header as='h2' textAlign='center'>Please Log In or Register</Header>
</div>
);
}
render() {
return (
<div>
{ this.homePage() }
</div>
);
}
}
const mapStateToProps = state => {
return { user: state.user };
};
export default Home;
自从我从头开始启动一个新应用程序已经有一段时间了,可能我缺少了一些非常基本的东西。
这是完整的错误:
×
TypeError: Cannot read property 'id' of undefined
Home._this.homePage
src/components/Home.js:9
6 | homePage = () => {
7 | debugger
8 | const { user } = this.props;
> 9 | if (user.id) {
10 | return (
11 | <div>
12 | <div>Games</div>
View compiled
Home.render
src/components/Home.js:33
30 | render() {
31 | return (
32 | <div>
> 33 | { this.homePage() }
34 | </div>
35 | );
36 | }
最后是终端错误:
Started GET "/api/auth/validate_token" for 127.0.0.1 at 2018-10-18 09:22:43 -0600
Processing by DeviseTokenAuth::TokenValidationsController#validate_token as HTML
/Users/michellegarcia/personal_projects/board_game_app/app/controllers/application_controller.rb:6: warning: already initialized constant ApplicationController::NameError
/Users/michellegarcia/personal_projects/board_game_app/app/controllers/application_controller.rb:6: warning: previous definition of NameError was here
uninitialized constant ApplicationController::Api
User Load (3.7ms) SELECT "users".* FROM "users" WHERE "users"."uid" = $1 LIMIT $2 [["uid", "test@test.com"], ["LIMIT", 1]]
↳ /Users/michellegarcia/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activerecord-5.2.1/lib/active_record/log_subscriber.rb:98
(0.3ms) BEGIN
↳ /Users/michellegarcia/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activerecord-5.2.1/lib/active_record/log_subscriber.rb:98
User Load (1.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 FOR UPDATE [["id", 1], ["LIMIT", 1]]
↳ /Users/michellegarcia/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activerecord-5.2.1/lib/active_record/log_subscriber.rb:98
User Update (1.5ms) UPDATE "users" SET "tokens" = $1, "updated_at" = $2 WHERE "users"."id" = $3 [["tokens", "{\"8ruFDYLiKIRAhTy1Cc0tnw\":{\"token\":\"$2a$10$3UtLDmi5ljtUJOfKwzx.y.njRRkZiFNoyYOBi35FGL6RxkHcsBHye\",\"expiry\":1541085763,\"last_token\":\"$2a$10$OiJjztMk7.bYuWxgwHKYmewx8T5xO00IM6iaLxQ/pMAsSdNwagy9W\",\"updated_at\":\"2018-10-18T09:22:43.556-06:00\"}}"], ["updated_at", "2018-10-18 15:22:43.659159"], ["id", 1]]
↳ /Users/michellegarcia/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activerecord-5.2.1/lib/active_record/log_subscriber.rb:98
(8.3ms) COMMIT
↳ /Users/michellegarcia/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activerecord-5.2.1/lib/active_record/log_subscriber.rb:98
Completed 200 OK in 289ms (Views: 1.6ms | ActiveRecord: 15.1ms)
Started GET "/%3Canonymous%3E" for 127.0.0.1 at 2018-10-18 09:22:46 -0600
Processing by StaticController#index as */*
Parameters: {"other"=>"<anonymous>"}
Completed 500 Internal Server Error in 186ms (ActiveRecord: 0.0ms)
ActionView::MissingTemplate (Missing template Users/michellegarcia/personal_projects/board_game_app/public/index.html with {:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :vcf, :vtt, :png, :jpeg, :gif, :bmp, :tiff, :svg, :mpeg, :mp3, :ogg, :m4a, :webm, :mp4, :otf, :ttf, :woff, :woff2, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip, :gzip], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby]}. Searched in:
* "/Users/michellegarcia/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-5.2.1/lib/rails/templates"
* "/Users/michellegarcia/personal_projects/board_game_app"
* "/"
):
app/controllers/static_controller.rb:7:in `index'
Postgres肯定已经启用,并且我肯定有一个用户。
提前谢谢!
答案 0 :(得分:1)
您似乎可能需要将home组件连接到redux store?
import { connect } from 'react-redux';
export default connect(mapStateToProps)(Home);