Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 在成功回调中调用yield put()_Reactjs_Redux_Yield_Amazon Cognito_Redux Saga - Fatal编程技术网

Reactjs 在成功回调中调用yield put()

Reactjs 在成功回调中调用yield put(),reactjs,redux,yield,amazon-cognito,redux-saga,Reactjs,Redux,Yield,Amazon Cognito,Redux Saga,我是全新的反应和重复 我使用react-redux调用AWS Cognito服务,该服务接受一个包含成功和失败回调的对象。当我在成功回调中输入console.log时,我从AWS Cognito获取JWT;但是,既然它不是生成器函数(function*),我怎么能在这个回调中yield put() 下面是一些代码: 如果您使用的是redux saga(非常棒),那么可以使用将类似cognitoUser.authenticateUser的asyc回调转换为一组由middlewhere执行的指令 当

我是全新的反应和重复

我使用react-redux调用AWS Cognito服务,该服务接受一个包含成功和失败回调的对象。当我在成功回调中输入console.log时,我从AWS Cognito获取JWT;但是,既然它不是生成器函数(
function*
),我怎么能在这个回调中
yield put()

下面是一些代码:
如果您使用的是redux saga(非常棒),那么可以使用将类似cognitoUser.authenticateUser的asyc回调转换为一组由middlewhere执行的指令

当中间件解析调用时,它将通过生成器单步执行下一个yield语句,您可以将返回结果分配给一个变量,然后使用put effect将该变量置于您的状态

export function* getAWSToken(){
  // username, password and userPool come from react state
  // not showing code for brevity.

  const userData = {
      Username: username,
      Pool: userPool,
    };
  const authenticationData = {
    Username : username,
    Password : password,
  };

  const cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
  const authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);

  // Note: since you're invoking an object method
  // you'll want to pass the object's context (this) with the call via an array

  const token = yield apply(cognitoUser, cognitoUser.authenticateUser, [authenticationDetails, { onSuccess(response){return response}, onFailure(){} }]);

  yield put(cognitoTokenObtained(token.getIdToken().getJwtToken());
}
还有一个我极力推荐的


编辑:为了简洁起见,您遗漏了一些代码,但我强烈建议将生成器中的代码包装到try-catch中,因为您依赖于API的外部IO。

为什么要使用生成器函数?如果每次你调用
getAWSToken
,cognito都会给你一个新的标记,生成器会改进吗?如前所述,我对React和Redux非常熟悉,所以我下面举几个例子。似乎只要使用redux传奇,就会使用生成器函数。如果不是这样的话,请告诉我。嗯,我觉得太复杂了。您可以使用一个普通函数,将回调传递给
getAWSToken(yourCallback)
,然后在
onSuccess
中调用该函数,就像
callback(result)
一样。但是,如果使用生成器函数有一些特定的原因,那么我不想建议一些破坏redux saga的东西。为什么你说你不能在回调中使用
yield
?你有什么错误吗?因为回调不是生成器,但您的
getAWSToken
是生成器,所以您不应该对这里的
yield
有任何问题。感谢您的示例和教程链接,我已经了解了更多关于
call/put
的信息,而我之前就知道了;但是,我仍然对您提供的示例有一点疑问。当我使用call for
cognitoUser.authenticateUser
时,AWS cognito JS库抱怨
TypeError:无法读取未定义的
的属性“getUserPoolId”,这让我觉得我没有将参数正确地传递到
call()
方法中。解决了这个问题,我需要传入上下文,即cognitoUser。我的电话是这样的
const-token=yield调用([cognitoUser,cognitoUser.authenticateUser],authenticationDetails)啊!对不起,我错过了!我将为未来的观众更新答案。谢谢你,帕特里克!还有一件事。此AWS调用希望访问authenticationDetails(作为参数传递)的上下文。我也可以传递这个上下文吗?顺便说一句,您可以使用
apply(context,fn,args)
而不是
call([context,fn],args)
authenticationDetails本身就是一个上下文(上下文是一个对象)。您必须在API调用中单独传递它们,因为该调用将方法调用的定义构建到一个不知道实例本身的函数中。这是不是给了你一个有用的错误?
export function* getAWSToken(){
  // username, password and userPool come from react state
  // not showing code for brevity.

  const userData = {
      Username: username,
      Pool: userPool,
    };
  const authenticationData = {
    Username : username,
    Password : password,
  };

  const cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
  const authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);

  // Note: since you're invoking an object method
  // you'll want to pass the object's context (this) with the call via an array

  const token = yield apply(cognitoUser, cognitoUser.authenticateUser, [authenticationDetails, { onSuccess(response){return response}, onFailure(){} }]);

  yield put(cognitoTokenObtained(token.getIdToken().getJwtToken());
}