[英]Why is console log returning a Proxy Object?
所以這是一個非常奇怪的事件,我不太確定我的代碼中發生了什么。 本質上,用戶輸入客戶 ID/潛在客戶 ID,並可以從該輸入中搜索該客戶的信息。 無論出於何種原因,當我調用我的方法 GetLeadInfo(leadID) 並在控制台記錄 LeadID 的值時,我都會得到這個具有各種值的代理對象。 我原以為只得到一個整數,結果卻得到了。 我以前從未見過這個,也不知道為什么這個方法會檢索這個奇怪的值。
這是 myfile.js
import React from 'react';
export default Class MyFile extends React.Component{
constructor(props){
super(props);
this.state = {
LeadID: "",
CustomerFirstName: "",
CustomerLastName: "",
CustomerAddress: "",
CustomerState: "",
CustomerCity: "",
CustomerZip: "",
CustomerEmail: "",
BusinessName: "",
BusinessAddress: "",
}
}
GetLeadInfo(leadID){
console.log(leadID);
let firstName = "";
let lastName = "";
let customerID = "";
let customerAddress = "";
let customerCity = ""
let customerState = "";
let customerZip = "";
let customerEmail = "";
$.get('/crm/leads/' + leadID).then(response => {
firstName = response.extended_info.fields_by_name["Customer First Name"];
lastName = response.extended_info.fields_by_name["Customer Last Name"];
customerID = response.extended_info.fields_by_name["Customer Number"];
customerAddress = response.extended_info.fields_by_name["Street"];
customerCity = response.extended_info.fields_by_name["City"];
customerState = response.extended_info.fields_by_name["State"];
customerZip = response.extended_info.fields_by_name["Zip"];
customerEmail = response.extended_info.fields_by_name["Email"];
if(this.state.searchCondition === "StoresWithinCustomer"){
this.SaveCoordsForCustomer(response);
}
this.setState({CustomerInfo: response,
CustomerID: customerID.trim(),
CustomerName: firstName + " " + lastName,
CustomerAddress: customerAddress,
CustomerCity: customerCity,
CustomerState: customerState,
CustomerZip: customerZip,
CustomerEmail: customerEmail,
}, function (){
this.getLastAd2(); // needed callback to retrieve info after setState
});
//console.log(response);
}).catch(error =>{
console.log("Error: " + error);
});
}
LeadIDHandler(e){
let lead = e.target.value;
this.setState({LeadID: lead});
}
render(){
return(
<div className="MainContainer">
<div className="Content">
<label style={{display: 'block', marginBottom: '5px'}}>Search by Lead ID</label>
<input type="text" style={{width: '10%'}} id="LeadID" onChange={this.LeadIDHandler.bind(this)}></input>
<button onClick={this.GetLeadInfo.bind(this.state.LeadID)}>Search</button>
<label style={{display: 'block', marginBottom: '5px'}}>Customer Number (if applicable)</label>
<input type="text" style={{width: '10%'}} id="CustomerNumber" value={this.state.CustomerID}></input>
<label style={{display: 'block', marginBottom: '5px'}}>Customer Name</label>
<input type="text" style={{width: '10%'}} id="CustomerName" onChange={this.CustomerInfoHandler.bind(this)} style={{display: 'block'}} value={this.state.CustomerName}></input>
<label style={{display: 'block', marginBottom: '5px'}}>Business Name</label>
<input type="text" style={{width: '10%'}} id="BusinessName" onChange={this.CustomerInfoHandler.bind(this)} style={{display: 'block'}} value={this.state.BusinessName}></input>
<label style={{display: 'block', marginBottom: '5px'}}>Business Address</label>
<input type="text" style={{width: '10%'}} id="BusinessAddress" onChange={this.CustomerInfoHandler.bind(this)} style={{display: 'block'}} value={this.state.BusinessAddress}></input>
<label style={{display: 'block', marginBottom: '5px'}}>City</label>
<input type="text" style={{width: '10%'}} id="CustomerCity" onChange={this.CustomerInfoHandler.bind(this)} style={{display: 'block'}} value={this.state.CustomerCity}></input>
<label style={{display: 'block', marginBottom: '5px'}}>State</label>
<input type="text" style={{width: '10%'}} id="CustomerState" onChange={this.CustomerInfoHandler.bind(this)} style={{display: 'block'}} value={this.state.CustomerState}></input>
<label style={{display: 'block', marginBottom: '5px'}}>Zip</label>
<input type="text" style={{width: '10%'}} id="CustomerZip" onChange={this.CustomerInfoHandler.bind(this)} style={{display: 'block'}} value={this.state.CustomerZip}></input>
</div>
</div>
);
}
}
如果有人知道為什么會發生這種情況,我將不勝感激! 謝謝。
傳遞給.bind()
的第一個參數不是 object 中的函數在調用時將獲得的第一個參數。
所以用這個代碼:
this.GetLeadInfo.bind(this.state.LeadID)
該函數的this
對象綁定到LeadID
,並且該函數將被調用而無需來自您的顯式參數LeadID
將添加它自己的參數,包括一個事件對象,這是您在控制台中看到的。
你應該做:
this.GetLeadInfo.bind(this, this.state.LeadID)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.