我正在开发一个React应用程序,该应用程序使用导入的对象以及对api的获取请求和对相关API的发布请求。
在React的前端中创建服务的新实例时,我能够成功地使用'.then'和'.catch'函数来仅从get请求访问返回的数据。
当使用来自同一对象的发布请求时,尝试访问响应对象时,我得到了(反义)“。then”,它不是未定义的函数。
只有当我在表单提交功能中显式写出发布请求(不使用服务)并处理对象时,我才能检查响应并随后设置状态。
在React中使用axios的适当/最佳实践方法是什么?为什么在创建服务的新实例时不能访问响应对象?非常感激!
服务:
import axios from 'axios';
class ProductServices {
getAllProducts(){
return axios.get('https://somecustomAPIURL')
}
postProduct(somePathConfig){
axios.request({
url: 'https://somecustomAPIURL' + somePathConfig,
method: 'post',
headers: {'some-custom-header': process.env.REACT_APP_API_POST_KEY}
})
}
}
export default ProductServices;
Run Code Online (Sandbox Code Playgroud)
React Code instantiating and consuming the service (note, that getAllProducts works just fine, but trying to consume a response object in postProduct returns an '.then' is undefined)
constructor(){
super();
this.state = {
products: [],
productID: null,
showModal: false
}
this.ProductServices = new ProductServices();
}
getAllProducts = …Run Code Online (Sandbox Code Playgroud) 我正在学习如何使用无服务器框架来创建和管理 AWS 服务。我已经使用无服务器站点上的文档逐步部署了无服务器项目,但由于某种原因,我无法在 AWS 管理控制台中看到 DynamoDB 表。
我已检查我使用的 AWS 配置文件是否正确,当我从终端使用 cURL 时,我能够从表中发布和获取数据,并且能够在浏览器中查看这些端点上的数据,但我无法在 serverless.yml 文件之外的任何地方看到对创建的表的任何引用。这是为什么?请参阅下面的代码(完整的演示存储库位于此链接: https: //github.com/serverless/examples/tree/master/aws-node-rest-api-with-dynamodb)。
感谢您帮助学习这里的细微差别。谢谢!
org: justinbell714
app: jb-test-from-docs
service: serverless-rest-api-with-dynamodb
frameworkVersion: ">=1.1.0 <2.0.0"
provider:
name: aws
runtime: nodejs10.x
environment:
DYNAMODB_TABLE: ${self:service}-${opt:stage, self:provider.stage}
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
Resource: "arn:aws:dynamodb:${opt:region, self:provider.region}:*:table/${self:provider.environment.DYNAMODB_TABLE}"
functions:
create:
handler: todos/create.create
events:
- http:
path: todos
method: post
cors: true
list:
handler: todos/list.list
events:
- http:
path: todos
method: get
cors: …Run Code Online (Sandbox Code Playgroud)