简体   繁体   English

AWS AmplifyAUTH: flutter: [ERROR]: CreateAuthChallenge 失败,出现错误 SyntaxError: await is only valid in async function

[英]AWS AmplifyAUTH: flutter: [ERROR]: CreateAuthChallenge failed with error SyntaxError: await is only valid in async function

Getting the captioned error message onTap signUpButton() within AuthenticatorForm().在 AuthenticatorForm() 中获取带标题的错误消息 onTap signUpButton()。 Seems like a very idiomatic error message, but can't seem to find what's wrong.似乎是一个非常惯用的错误消息,但似乎无法找到问题所在。

Here is my createAuthChallenge.js这是我的 createAuthChallenge.js

const AWS = require('aws-sdk');
const digitGenerator = require('crypto-secure-random-digit');

function sendSMS(phone, code) {
  const params = {
    Message: code,
    PhoneNumber: phone,
  };
  return new AWS.SNS({apiVersion: '2010-03-31'}).publish(params).promise();
}

async function createAuthChallenge(event) {
  if (event.request.challengeName === 'CUSTOM_CHALLENGE') {
    const randomDigits = digitGenerator.randomDigits(6).join('');
    const challengeCode = String(randomDigits).join('');
    await sendSMS(event.request.userAttributes.phone_number, challengeCode);
  
    event.response.privateChallengeParameters = {};
    event.response.privateChallengeParameters.answer = challengeCode;
  }
}

exports.handler = async (event) => {
  createAuthChallenge(event);
};

And my package.json for the same和我的 package.json 同样

{
  "name": "XYZ",
  "version": "2.0.0",
  "description": "Lambda function generated by Amplify",
  "main": "index.js",
  "license": "Apache-2.0",
  "devDependencies": {
    "@types/aws-lambda": "^8.10.92"
  },
  "dependencies": {
    "crypto-secure-random-digit": "^1.0.9"
  }
}

I can't seem to find the right solution for this, can anyone help please?我似乎找不到合适的解决方案,有人可以帮忙吗?

Please try to replace your last line with:请尝试将您的最后一行替换为:

exports.handler = createAuthChallenge;

If it doesn't work, can you add some other details?如果它不起作用,你可以添加一些其他细节吗?

Not really a useful answer, but as self-resolved.这不是一个真正有用的答案,而是自我解决的。

Ultimately just used the same codes that I copied above, but redid everything from amplify delete > amplify init > amplify add auth > amplify push.最终只是使用了我在上面复制的相同代码,但重新做了从放大删除 > 放大初始化 > 放大添加身份验证 > 放大推送的所有内容。 After this, no errors in simulator.在此之后,模拟器中没有错误。

If anyone runs into similar problems, the best approach based off my experience on this, is to check the cloudwatch logs of the lambdas fired.如果有人遇到类似问题,根据我的经验,最好的方法是检查被触发的 lambda 的 cloudwatch 日志。 In most cases, the logs should do a better job of pointing out where the issue is than the error messages in local.在大多数情况下,日志应该比本地的错误消息更好地指出问题所在。 If that doesn't work, backup everything and reset amplify.如果这不起作用,请备份所有内容并重置放大。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想用令牌重置密码但是 => SyntaxError: await is only valid in async function - i want to reset password with token but => SyntaxError: await is only valid in async function await 仅在异步函数和模块的顶层主体中有效 javascript 表示错误 - await is only valid in async functions and the top level bodies of modules javascript express error 嵌套异步等待 function 未在 AWS 中执行 Lambda Function - Nested async await function not executing in AWS Lambda Function ERROR 'await' 表达式只允许在异步函数中和模块的顶层 - ERROR 'await' expressions are only allowed within async functions and at the top levels of modules Firebase setDoc 导致 async/await 错误 - Firebase setDoc results in a async/await error 具有云功能的异步 function(异步语法错误) - async function with cloud functions (syntax error on async) 将 Firebase Function 转换为异步/等待 - Convert Firebase Function to Async/Await AWS Lambda 只读文件系统错误无法创建包含 Docker 图片的目录 - AWS Lambda read-only file system error failed to create directory with Docker image flutter 为什么异步和等待不起作用? - flutter Why doesn't async and await work? AWS Lambda - [错误](快速)无法保留:AlreadyReserved - AWS Lambda - [ERROR] (rapid) Failed to reserve: AlreadyReserved
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM