简体   繁体   English

如何修复“无法读取未定义的属性'sendMessage'”反应聊天系统

[英]How to fix “ Cannot read property 'sendMessage' of undefined” react chat system

I am building a chat system with react and chatkit. 我正在建立一个带有react和chatkit的聊天系统。 I have a function called sendMessage in App.js that sends whatever the user type in the sendMessage Form to display it in the MessageList Component. 我在App.js中有一个名为sendMessage的函数,该函数可以发送sendMessage表单中的任何用户类型以将其显示在MessageList组件中。

this is App.js 这是App.js

import Chatkit from '@pusher/chatkit'
import MessageList from './components/MessageList'
import SendMessageForm from './components/SendMessageForm'
import RoomList from './components/RoomList'
import NewRoomForm from './components/NewRoomForm'

import { tokenUrl, instanceLocator } from './config'

class App extends React.Component {

    constructor() {
        super()
        this.state = {
            messages: []
        }
        this.sendMessage = this.sendMessage.bind(this)
    } 

    componentDidMount() {
        const chatManager = new Chatkit.ChatManager({
            instanceLocator,
            userId: 'Ismail',
            tokenProvider: new Chatkit.TokenProvider({
                url: tokenUrl
            })
        })

        chatManager.connect()
        .then(currentUser => {
            this.currentUser = currentUser
            this.currentUser.subscribeToRoom({
                roomId: 24622056,
                hooks: {
                    onNewMessage: message => {
                        this.setState({
                            messages: [...this.state.messages, message]
                        })
                    }
                }
            })
        })
    }

    sendMessage(text) {
        this.currentUser.sendMessage({
            text,
            roomId: 24622056
        })
    }

    render() {
        return (
            <div className="app">
                <RoomList />
                <MessageList messages={this.state.messages} />
                <SendMessageForm sendMessage={this.sendMessage} />
                <NewRoomForm />
            </div>
        );
    }
}

export default App

And this is sendMessageForm.js component. 这是sendMessageForm.js组件。

import React from 'react'

class SendMessageForm extends React.Component {

    constructor() {
        super()
        this.state = {
            message: ''
        }
        this.handleChange = this.handleChange.bind(this)
        this.handleSubmit = this.handleSubmit.bind(this)
    }

    handleChange(e) {
        this.setState({
            message: e.target.value
        })
    }

    handleSubmit(e) {
        e.preventDefault()
        this.props.sendMessage(this.state.message)
    }

    render() {
        return (
            <form
                onSubmit={this.handleSubmit}
                className="send-message-form">
                <input
                    onChange={this.handleChange}
                    value={this.state.message}
                    placeholder="Type your message and hit ENTER"
                    type="text" />
            </form>
        )
    }
}

export default SendMessageForm

the expected output is the message showing up in the messages box after i hit enter but I get this: 预期的输出是在我按Enter键后显示在消息框中的消息,但是我得到了:

TypeError: Cannot read property 'sendMessage' of undefined
App.sendMessage
src/App.js:46
  43 | }
  44 | 
  45 | sendMessage(text) {
> 46 |     this.currentUser.sendMessage({
     | ^  47 |         text,
  48 |         roomId: 24622056
  49 |     })
View compiled
SendMessageForm.handleSubmit
src/components/SendMessageForm.js:22
  19 | 
  20 | handleSubmit(e) {
  21 |     e.preventDefault()
> 22 |     this.props.sendMessage(this.state.message)
     | ^  23 | }
  24 | 
  25 | render() {

As others have answered, your currentUser class property is undefined, and as such can't have a sendMessage method. 正如其他人回答的那样,您的currentUser类属性是未定义的,因此不能具有sendMessage方法。 Your code essentially looks like this: 您的代码基本上如下所示:

this.undefined.sendMessage(...)

which obviously can't work. 这显然是行不通的。

You probably forgot to set currentUser 's value (I guess it has a sendMessage method). 您可能忘记了设置currentUser的值(我猜它具有sendMessage方法)。

In your sendMessageForm.js File Change the first two lines of constructor to this 在您的sendMessageForm.js文件中,将构造函数的前两行更改为

constructor(props) {
super(props);
.
. 
.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复无法读取未定义 react-redux 的属性“名称” - how to fix Cannot read property 'name' of undefined react-redux 如何修复:React DOM 库的“无法读取未定义的属性‘forEach’” - How to fix: 'Cannot read property 'forEach' of undefined' of react DOM library 如何解决discord.js中的“无法读取未定义的属性&#39;反应&#39;” - How to fix “Cannot read property 'react' of undefined” in discord.js 如何修复 React 项目中的“TypeError:无法读取未定义的属性‘继承’”? - How to fix “TypeError: Cannot read property 'inherits' of undefined” in React project? 如何使用 react 和 typescript 修复错误无法读取未定义的属性? - How to fix error cannot read property of undefined using react and typescript? 如何在反应中修复“无法读取未定义的属性&#39;评论&#39;” - how to fix "Cannot read property 'comments' of undefined" in react 如何修复无法读取React中未定义的&#39;exportPDFWithComponent&#39;属性? - How to fix Cannot read property 'exportPDFWithComponent' of undefined in React? 如何修复 TypeError:无法读取未定义的属性? - How to fix TypeError: Cannot read property of undefined? 如何修复无法读取未定义的属性“纬度” - How to fix Cannot read property 'Latitude' of undefined 如何解决:“无法读取未定义的属性&#39;then&#39;” - how to fix: 'Cannot read property 'then' of undefined'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM