2017-07-07 158 views
-1

我在我的Meteor React應用程序中遇到了這個錯誤。長話短說:我有一個表單來創建一個事件,我希望handleSubmit()來處理錯誤消息,如果沒有,向db添加事件。我確實導入了{Events},事實上這個表單在我做了一些修改之前就已經工作了。當我運行它時,我收到一條錯誤消息:Uncaught TypeError:event.target [matches]不是函數。拋出錯誤的文件不幸的是對我來說很亂。表單提交錯誤

這裏是我的代碼:

export default class Create extends React.Component { 
constructor(props) { 
    super(props); 
    this.state = { 
     error: {} 
    } 

    this.handleSubmit = this.handleSubmit.bind(this); 
} 
handleSubmit(evt) { 
    evt.preventDefault(); 

    this.setState({error: {}}); 
    let title = this.refs.title.value; 
    if (!title) { 
    this.setState((prevState) => { 
     let newState = prevState; 
     newState.error.title = 'Title has to be filled up.'; 
     return newState; 
    }) 
    } 
    let description = this.refs.description.value; 
    if (!description) { 
    this.setState((prevState) => { 
     let newState = prevState; 
     newState.error.description = 'Description has to be filled up.'; 
     return newState; 
}) 
} 

if (!this.state.error) { 
    Events.insert({title: title, description: description}); 
    this.props.history.push('/home'); 
} 

和我的html:

<form onSubmit={this.handleSubmit} noValidate> 

     <input ref="title" type="text" name="title" placeholder="Title" 
     style={this.state.error.title ? {borderBottomColor: 'red'} : undefined}/> 
     <div className="errorText">{this.state.error.title}</div> 

     <input ref="description" type="text" name="description" placeholder="Description" 
     style={this.state.error.description ? {borderBottomColor: 'red'} : undefined}/> 
     <div className="errorText">{this.state.error.description}</div> 

     <button type="submit" className="btn btn-success">Create new event</button> 

    </form> 

可惜我不能進行該項目,直到這個錯誤解決了,所以我會很高興,如果有人幫我!

最佳,

+0

嗨,湯姆,謝謝你花時間。它是重複的。另一個是沒有答案......我等了30分鐘沒有一個新的觀點,所以我猜它已經死了,問題仍然相關。你可以取消你的失敗,因爲它沒有幫助。最好的, –

回答

0

好像不支持你目標元素element.matches功能,

我未清楚這是否是由流星本地腳本造成的,因爲我沒有使用過的流星。

但是,您可以通過添加此代碼並添加您自己的原型來繞過此錯誤進行檢查。

if (!Element.prototype.matches) { 
Element.prototype.matches = 
    Element.prototype.matchesSelector || 
    Element.prototype.mozMatchesSelector || 
    Element.prototype.msMatchesSelector || 
    Element.prototype.oMatchesSelector || 
    Element.prototype.webkitMatchesSelector || 
    function(s) { 
     var matches = (this.document || this.ownerDocument).querySelectorAll(s), 
      i = matches.length; 
     while (--i >= 0 && matches.item(i) !== this) {} 
     return i > -1;    
    }; 
} 

SOURCES

我希望可以解決您的問題。

+0

嘿Vipul,非常感謝您花時間。我剛發現的問題,我即將發佈在這裏https://stackoverflow.com/questions/44967707/event-targetmatches-error-on-form-submit-in-meteor-react-project –