Javascript 在函数内传递本机变量
我有一个函数,我想在这个函数中传递一个变量。 此sendDraft函数将接收参数Javascript 在函数内传递本机变量,javascript,reactjs,react-native,binding,frontend,Javascript,Reactjs,React Native,Binding,Frontend,我有一个函数,我想在这个函数中传递一个变量。 此sendDraft函数将接收参数 sendDraft(billnumber) { axios .post("http://192.168.0.111:3000/sendDraft", { invoice: billnumber, }) .then((response) => { console.log(response); });
sendDraft(billnumber) {
axios
.post("http://192.168.0.111:3000/sendDraft", {
invoice: billnumber,
})
.then((response) => {
console.log(response);
});
}
这是将参数发送到sendDraft函数的函数
let drafts = this.state.invoice.map((invc) => {
return (
<Card key={invc.invoiceId}>
<Card.Title>Invoice Number: {invc.billnumber}</Card.Title>
<Card.Divider />
<View>
<Text>
Customer's address:
{invc.address}
</Text>
<Text>Customer's email: {invc.email}</Text>
<Text>Total Price: {invc.price}</Text>
</View>
<Button //Button which will be pressed to send parameter
title="SEND DRAFT"
onPress={this.sendDraft.bind(this, invc.billnumber)}
/>
</Card>
);
});
let drafts=this.state.invoice.map((invc)=>{
返回(
发票编号:{invc.billnumber}
客户地址:
{invc.address}
客户电子邮件:{invc.email}
总价:{invc.Price}
);
});
那是按钮(最后一个按钮)
我确信invc.billnumber不是空的
但是当我点击按钮时,变量没有被传递,update语句也找不到记录
(API在《邮递员》中非常有效)
问题是什么?我们如何在react native中传递变量。要确认invc.billnumber不是null或未定义,您必须调试它们。 返回前控制台检查invc.billnumber是否为空或未定义
console.log(invc.billnumber);
第二个问题可能是实现onPress事件的绑定。
您可以使用ES6语法在构造函数中进行绑定:
export default class Abc extends Component {
constructor(props) {
super(props);
this.sendDraft= this.sendDraft.bind(this);
}
然后:
let drafts = this.state.invoice.map((invc) => {
console.log(invc.billnumber); //To check invc.billnumber value
return (
<Card key={invc.invoiceId}>
<Card.Title>Invoice Number: {invc.billnumber}</Card.Title>
<Card.Divider />
<View>
<Text>
Customer's address:
{invc.address}
</Text>
<Text>Customer's email: {invc.email}</Text>
<Text>Total Price: {invc.price}</Text>
</View>
<Button
title="SEND DRAFT"
onPress={() => this.sendDraft(invc.billnumber)}> //Bind like this
/>
</Card>
);
});
let drafts=this.state.invoice.map((invc)=>{
console.log(invc.billnumber);//检查invc.billnumber值
返回(
发票编号:{invc.billnumber}
客户地址:
{invc.address}
客户电子邮件:{invc.email}
总价:{invc.Price}
this.sendDraft(invc.billnumber)}>//像这样绑定
/>
);
});
sendDraft(billnumber){console.log(billnumber);axios.post(“http://192.168.0.111:3000/sendDraft“,{invoice:billnumber,})。然后((response)=>{console.log(response);});}
我这样做了,console.log()返回了一个值,所以即使在函数中,billnumber也不是空的,我按照您在回答中所说的做了,这可能是billnumber现在显示感谢的原因!好啊继续讨论约束问题。您似乎没有正确绑定和发送参数。阅读我答案中的第二个问题。我可以私下再问你一个问题吗?@jayzee好的,我可以寻求帮助!