2016-02-14 91 views
0

因此,我有一個Firebase設置,我試圖從snapshot.val()中將我的數據放入this.props.device,但是' this'在'this.firebaseRef.on()函數內部是'null'。有任何想法嗎?我如何從Firebase獲取數據並將其放入this.props - React

var RegisteredDevice = React.createClass({ 
    mixins: [ReactFireMixin], 

    componentWillMount: function() { 
     this.firebaseRef = new Firebase("https://omniwolfdsn.firebaseio.com/Devices"); 

     this.firebaseRef.on('value', function(snapshot) { 
     this.props.device = snapshot.val(); 
     }); 
    }, 

    propTypes: { 

     name: React.PropTypes.string.isRequired, 
     device: React.PropTypes.string, 
     fire: React.PropTypes.string, 
     noise: React.PropTypes.string, 
     motion: React.PropTypes.string, 
    } 
)}; 

回答

0

您可以通過this作爲第四個參數

this.firebaseRef.on('value', function(snapshot) { 

    this.props.device = snapshot.val(); 

    }, (error) => console.log(error), this); 

按照文檔的一次簽名是

on(eventType, successCallback, [failureCallback], [context]) 

所以第四個參數可以this

+0

@AndrewSteinheiser這個可以是第四個參數。 –

+0

我沒有收到錯誤,但是,當我將它記錄到函數之外時,this.props.device未定義。 – AndrewSteinheiser

+0

@AndrewSteinheiser是你使用'getDefaultProps'嗎? –

相關問題