AWS 放大 NoCredentials:凭证不应为空

问题描述 投票:0回答:1

伙计们,我是新的 aws amplify,我的目标是从 api 网关获取 api 并连接到我的 lambda 函数,不幸的是,由于此错误 NoCredentials:凭证不应为空,我可以访问 api。,我已经设置了我的 aws 配置,但是仍然发生在这里是我的示例代码

应用程序.js

import { get } from "aws-amplify/api";
import { useState } from "react";

const myAPI = "api1c2a0195";
const path = "/customers";

const App = () => {
  const [input, setInput] = useState("");
  const [customers, setCustomers] = useState([]);

  //Function to fetch from our backend and update customers array
  async function getCustomer(e) {
    let customerId = e.input;
    try {
      const restOperation = get({
        apiName: myAPI,
        path: path + "/" + customerId,
      });

      const response = await restOperation.response;
      console.log(response);
      let newCustomers = [...customers];
      newCustomers.push(response);
      setCustomers(newCustomers);
    } catch (error) {
      console.log("GET call failed: ", error);
    }
  }

  return (
    <div className="App">
      <h1>Super Simple React App</h1>
      <div>
        <input
          placeholder="customer id"
          type="text"
          value={input}
          onChange={(e) => setInput(e.target.value)}
        />
      </div>
      <br />
      <button onClick={() => getCustomer({ input })}>
        Get Customer From Backend
      </button>

      <h2 style={{ visibility: customers.length > 0 ? "visible" : "hidden" }}>
        Response
      </h2>
      {customers.map((thisCustomer, index) => {
        return (
          <div key={thisCustomer.customerId}>
            <span>
              <b>CustomerId:</b> {thisCustomer.customerId} - <b>CustomerName</b>
              : {thisCustomer.customerName}
            </span>
          </div>
        );
      })}
    </div>
  );
};

export default App;

main.js

import React from "react";
import ReactDOM from "react-dom/client";
import { Amplify } from "aws-amplify";
import amplifyconfig from "./amplifyconfiguration.json";
import App from "./App.jsx";
import "./index.css";

Amplify.configure(amplifyconfig);

ReactDOM.createRoot(document.getElementById("root")).render(
  <React.StrictMode>
    <App />
  </React.StrictMode>
);
reactjs amazon-web-services aws-amplify
1个回答
0
投票

当我使用 REST API 处理 vue 的示例代码时,我遇到了同样的错误。通过将 amplify auth 类别添加到我的项目中,我能够克服该错误

$ amplify add auth
$ amplify push

使用 vue graphql api 示例代码无需添加身份验证即可工作。

© www.soinside.com 2019 - 2024. All rights reserved.