我正在使用Dialogflow和Firebase开发一个react,redux聊天机器人。我正在跟踪所有对话以改进机器人。
当有人第一次发送消息时,会在数据库中创建一个新会话,其中createNewConversation
后跟sendMessage
函数将消息推送到数据库。
问题是conversationId
函数首次触发时仍未定义sendMessage
。
我认为这可以通过使用then
来解决,但我收到错误(然后是未定义的):
if(this.props.isNewConversation) {
this.props.createNewConversation()
.then (() => {
this.props.sendMessage(this.props.conversationId, message);
});
}
我需要做些什么来改变这项工作?另外,这可以通过async / await来完成吗?
createNewConversation& setConversationId Action
export const createNewConversation = () => dispatch => {
Database.ref('chatbot/conversations').push()
.then(response => {
dispatch(setConversationId(response.key));
})
}
export const setConversationId = id => ({
type: SET_CONVERSATION_ID,
id
});
答案 0 :(得分:2)
您的createNewConversation
并未返回任何内容,只需添加return
声明即可。
export const createNewConversation = () => dispatch => {
return Database.ref('chatbot/conversations').push()
.then(response => {
dispatch(setConversationId(response.key));
})
}
答案 1 :(得分:0)
使用redux生命周期。您目前正在尝试使用promises绕过Redux。
sendMessage
如果componentDidUpdate
未归零/未定义或符合您需要的条件,则conversationId
会触发int find_pid_of(const char *process_name)
{
int id;
pid_t pid = -1;
DIR* dir;
FILE *fp;
char filename[32];
char cmdline[256];
struct dirent * entry;
if (process_name == NULL)
return -1;
dir = opendir("/proc");
if (dir == NULL)
return -1;
while((entry = readdir(dir)) != NULL) {
id = atoi(entry->d_name);
if (id != 0) {
sprintf(filename, "/proc/%d/cmdline", id);
fp = fopen(filename, "r");
if (fp) {
fgets(cmdline, sizeof(cmdline), fp);
fclose(fp);
if (strcmp(process_name, cmdline) == 0) {
/* process found */
pid = id;
break;
}
}
}
}
closedir(dir);
return pid;
}
。
答案 2 :(得分:0)
您需要从export const createNewConversation = () => dispatch => {
return Database.ref('chatbot/conversations').push()
.then(response => {
dispatch(setConversationId(response.key));
})
}
export const setConversationId = id => ({
type: SET_CONVERSATION_ID,
id
});
返回承诺,例如:
db.coll.update({"title" : "Document"},
{$push:{"comments":{
"user":'user2',
"message": 'My second comment',
"dateCreated": new Date(2013,11,10,2,35),
"like": 0
}}})
答案 3 :(得分:0)
您没有从createNewConversation
返回您的承诺,这就是为什么.then
无法正常工作的原因。你想要回复承诺:
export const createNewConversation = () => dispatch => {
return Database.ref('chatbot/conversations').push()
.then(response => {
dispatch(setConversationId(response.key));
})
}
export const setConversationId = id => ({
type: SET_CONVERSATION_ID,
id
});
使用async / await:
export const createNewConversation = async () => dispatch => {
return await Database.ref('chatbot/conversations').push()
.then(response => {
dispatch(setConversationId(response.key));
})
}
export const setConversationId = id => ({
type: SET_CONVERSATION_ID,
id
});