2016-09-20 252 views
0

我想将用户输入的ImageTag的值以及从下拉菜单(material-ui的selectField)中选择的locationValue传递给服务器插座。React JS Uncaught ReferenceError:未定义locationValue(变量)

这里是我的代码: -

var BaseImageDetails = React.createClass({ 
getInitialState:function(){ 
    return{ 
     imageTag: '', 
     locationValue: '' 
    }; 

}, 
contextTypes: { 
    socket: React.PropTypes.object.isRequired 
}, 

handleImageChange:function(event){ 
    this.setState({imageTag: event.target.value}); 
    console.log(imageTag); 
}, 

handleLocationChange:function(event){ 
    this.setState({locationValue: event.target.value}); 
    console.log(locationValue); 
}, 


clickedBase: function(e){ 
    e.preventDefault(); 
    this.context.socket.emit("baseImageSubmit",{imageTag},{locationValue}); 


}, 

render(){ 
    console.log("wwooowwowow" , this.props.locationDetails); 
     var locationItems = this.props.locationDetails.map(function(locationItem){ 
      return <MenuItem value = {locationItem} primaryText = {locationItem} /> 
     }.bind(this));  

    console.log("locationItems------------",locationItems); 
    return(
     <div>    
      <Card> 
       <CardHeader 
        title="Please provide the following details ? " 
        actAsExpander={true} 
        showExpandableButton={true} 
       /> 
       <form onSubmit = {this.clickedBase} > 
        <TextField hintText="Image Tag" 
        floatingLabelText="Image Tag" 
        value = {this.state.imageTag} onChange = {this.handleImageChange}/> 
        <Divider /> 
        <SelectField 
         fullWidth={true} 
         hintText="Select the location of your base-image Dockerfile" 
         onChange = {this.handleLocationChange} 
         value = {this.state.locationValue}                      
         maxHeight={200}> 
         {locationItems} 
        </SelectField> 
        <Divider /> 
        <RaisedButton label="Secondary" primary={true} 
        label="Build" secondary={true} 
        type = "submit" /> 
       </form>        
       </Card> 
      </div> 
     ); 
    } 
}); 

不过,虽然安慰它打印 “未捕获的ReferenceError:locationValue没有定义” 既为locationValue和imageTag。 你们能帮助我在哪里,我做错了....

+0

看来你行'this.context.socket.emit( “baseImageSubmit”,{imageTag},{locationValue });'应该改为'this.context.socket.emit(“baseImageSubmit”,{imageTag:this.state.imageTag},{locationValue:this.state.locationValue});' – Joy

+0

Yeah Man !!!!谢谢一吨 –

回答

0

当你写{imageTag},这是一样的书写{imageTag: imageTag},而且也没有在你的范围称为imageTag局部变量。

你大概的意思

this.context.socket.emit("baseImageSubmit",{ 
    imageTag: this.props.imageTag 
},{ 
    locationValue: this.props.locationValue 
}); 

或者,如果你要使用destructuring assignments

const {imageTag, locationValue} = this.props; 
this.context.socket.emit("baseImageSubmit",{imageTag},{locationValue}); 
+0

是的,我犯了这个错误,应该是上面提到的那个错误!!尽管如此..谢谢很多 –

+0

@ParamveerSingh然而什么? –

+0

你指出的错误是由我注意到,当写{imageTag}没有任何局部变量名为imageTag在同一范围内......这就是为什么我得到这个错误......谢谢你这么多解释:) –

相关问题