我正在尝试将数据添加到Cloud Firestore,但是在Button Click上出现此错误
TypeError: Cannot read property 'target' of undefined
这是代码。
function Note(props) {
function ReplyAnswer(e)
{
var date = moment().utcOffset('+05:30').format('hh:mm A DD-MM-YYYY');
firebase.firestore().collection('qna2dreply').add(
{
rcontent: e.target.value,
replyby: userName,
replybyid: userID,
replytoid: props.id,
time: new Date(),
time2: date
})
.then(() => {
document.getElementById('xxx').value = '';
swal("Successfully Replied to "+props.title);
});
}
return (
<div>
<div className="note">
<h1>{props.title} <span className="time"> {(props.time).toString()}</span></h1>
<p>{props.content}
<input
className="replyForminput"
name="content"
required
placeholder={'Reply to '+props.title}
autoCorrect="off"
autoComplete="off" />
<button onClick={() => { ReplyAnswer() }}
className="replyBtn"> Reply </button></p>
{props.id == idx ? (<div><RBox /></div>) : null}
</div>
</div>
);
}
请帮忙
................................................... ................................................... ................................................... ................................................... ................................................... ................................................... ................................................... ................................................... ................................................... ................................................... ................................................... ................................................... ......
You should only pass the
ReplyAnswer
to theonClick
event of thebutton
, don't wrap it in an arrow function:This will pass the event object to your
ReplyAnswer
as ane
parameter. If you wrap yourReplyAnswer
in an arrow function, and call it without passing any parameter to it, like what you have done:You can see that
ReplyAnswer
receives no parameter, so youre
isundefined
. Your arrow function is also a zero-parameter function, so it will not receive event object fromonClick
event.e.target.value未定义,因为未从事件处理程序(ReplyAnswer)中传递事件(e)。
只需更改:按钮onClick = {()=> {ReplyAnswer()}}
收件人:按钮onClick = {{e)=> ReplyAnswer(e)}
这样可以确保事件正确传递。请注意,花括号已删除,以防止从单击处理程序返回未定义的花括号。
干杯,