I building a chatbot and I have some problem with the conversation with the Bot. At some point of the conversation I saw error: TypeError: Cannot read property 'fields' of undefined
I building a chatbot and I have some problem with the conversation with the Bot. At some point of the conversation I saw error: TypeError: Cannot read property 'fields' of undefined
componentDidUpdate() {
this.scrollToBottom();
if ( this.talkInput ) {
this.talkInput.focus();
}
}
_handleInputKeyPress(e) {
if (e.key === 'Enter') {
this.df_text_query(e.target.value);
e.target.value = '';
}
}
_handleQuickReplyPayload(payload, text) {
switch (payload) {
case 'recommend_yes':
this.df_event_query('SHOW_RECOMMENDATIONS');
break;
case 'training_maybe':
this.df_event_query(text);
break;
default:
this.df_text_query(text);
}
}
renderCards(cards) {
return cards.map((card, i) => <Card key={i} payload={card.structValue}/>);
}
renderOneMessage(message, i) {
if (message.msg && message.msg.text && message.msg.text.text) {
return <Message key={i} speaks={message.speaks} text={message.msg.text.text}/>;
} else if (message.msg && message.msg.payload.fields.cards) { //message.msg.payload.fields.cards.listValue.values
return <div key={i}>
<div className="card-panel grey lighten-5 z-depth-1">
<div style={{overflow: 'hidden'}}>
<div className="col s2">
<a href="/" className="btn-floating btn-large waves-effect waves-light red">{message.speaks}</a>
</div>
<div style={{ overflow: 'auto', overflowY: 'scroll'}}>
<div style={{ height: 300, width:message.msg.payload.fields.cards.listValue.values.length * 270}}>
{this.renderCards(message.msg.payload.fields.cards.listValue.values)}
</div>
</div>
</div>
</div>
</div>
} else if (message.msg &&
message.msg.payload &&
message.msg.payload.fields &&
message.msg.payload.fields.quick_replies
) {
return <QuickReplies
text={message.msg.payload.fields.text ? message.msg.payload.fields.text : null}
key={i}
replyClick={this._handleQuickReplyPayload}
speaks={message.speaks}
payload={message.msg.payload.fields.quick_replies.listValue.values}/>;
}
}
renderMessages(returnedMessages) {
if (returnedMessages) {
return returnedMessages.map((message, i) => {
return this.renderOneMessage(message, i);
}
)
} else {
return null;
}
}
When I click on button "yes" bot need to give me some answer. But instead answer, I see this error on my page: https://prnt.sc/ox4i1r
You need to add extra checking for payload here:
} else if (message.msg && message.msg.payload.fields.cards) {
Like so :
} else if (message.msg && message.msg.payload && message.msg.payload.fields.cards) {
'use strict';
const chatbot = require('../chatbot/chatbot');
module.exports = app => {
app.get('/', (req, res) => {
res.send({'zdravo': 'ljudi'})
});
app.post('/api/df_text_query', async (req, res) => {
let responses = await chatbot.textQuery(req.body.text, req.body.userID, req.body.parameters);
res.send(responses[0].queryResult);
res.end();
});
app.post('/api/df_event_query', async (req, res) => {
let responses = await chatbot.eventQuery(req.body.event, req.body.userID, req.body.parameters);
res.send(responses[0].queryResult);
res.end();
});
}
This is nodejs code with df_event_query
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.