作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用swagger-react-native-client并尝试从petstore请求数据
我按照教程进行了
Swagger('https://petstore.swagger.io/v2/swagger.json')
.then((client) => {
console.log(client);
console.log('---client---');
client.apis.pet
.findByStatus({status: 'available'})
// client
// .execute({
// operationId: 'findByStatus',
// parameters: {status: 'available'},
// })
.then((result) => {
console.log('---result---');
console.log(result);
})
.catch((err) => {
console.log('---error----');
console.log(err.message);
throw err;
});
})
.catch((err) => {
console.log('---error----');
console.log(err.message);
throw err;
});
而我得到了错误
client.apis.pet.findByStatus is not a function. (In 'client.apis.pet.findByStatus({
status: 'available'
})', 'client.apis.pet.findByStatus' is undefined)
[Fri Sep 04 2020 12:33:27.700] WARN Possible Unhandled Promise Rejection (id: 0):
TypeError: client.apis.pet.findByStatus is not a function. (In 'client.apis.pet.findByStatus({
status: 'available'
})', 'client.apis.pet.findByStatus' is undefined)
此后,没有在线可用的教程。我只是尝试了一些代码,但它不起作用。
最佳答案
TL; DR
我认为您正在寻找函数findPetsByStatus
而不是findByStatus
:
Swagger('http://petstore.swagger.io/v2/swagger.json').then((client) => {
client.apis.pet.findPetsByStatus({status: 'available'}).then((result) => {
console.log(result);
});
});
/pet/findByStatus
和函数名称与此不符。
Swagger(...)
的响应(在您的示例中称为
client
)并查看此对象具有的属性和值来找到正确的函数名称:
Swagger('http://petstore.swagger.io/v2/swagger.json').then((client) => {
console.log(client)
});
Swagger(url, options)
返回
Promise
(
https://www.npmjs.com/package/swagger-react-native-client#constructor-and-methods)。
then
子句中,我们可以访问(成功)异步操作的结果值,即
Swagger(...)
调用。在示例中,您已将此结果称为
client
。
apis
的属性,该属性具有一个对象作为值。该对象包含您可以使用的所有与api相关的功能。
[Object]
时得到的格式和格式,略有调整(在
[Function anonymous]
和
client
实例中添加引号):
{
"apis": {
"pet": {
"addPet": "[Function anonymous]",
"deletePet": "[Function anonymous]",
"findPetsByStatus": "[Function anonymous]",
"findPetsByTags": "[Function anonymous]",
"getPetById": "[Function anonymous]",
"updatePet": "[Function anonymous]",
"updatePetWithForm": "[Function anonymous]",
"uploadFile": "[Function anonymous]"
},
"store": {
"deleteOrder": "[Function anonymous]",
"getInventory": "[Function anonymous]",
"getOrderById": "[Function anonymous]",
"placeOrder": "[Function anonymous]"
},
"user": {
"createUser": "[Function anonymous]",
"createUsersWithArrayInput": "[Function anonymous]",
"createUsersWithListInput": "[Function anonymous]",
"deleteUser": "[Function anonymous]",
"getUserByName": "[Function anonymous]",
"loginUser": "[Function anonymous]",
"logoutUser": "[Function anonymous]",
"updateUser": "[Function anonymous]"
}
},
"errors": [],
"originalSpec": "undefined",
"spec": {
"$$normalized": true,
"basePath": "/v2",
"definitions": {
"ApiResponse": "[Object]",
"Category": "[Object]",
"Order": "[Object]",
"Pet": "[Object]",
"Tag": "[Object]",
"User": "[Object]"
},
"externalDocs": {
"description": "Find out more about Swagger",
"url": "http://swagger.io"
},
"host": "petstore.swagger.io",
"info": {
"contact": "[Object]",
"description": "This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters.",
"license": "[Object]",
"termsOfService": "http://swagger.io/terms/",
"title": "Swagger Petstore",
"version": "1.0.5"
},
"paths": {
"/pet": "[Object]",
"/pet/findByStatus": "[Object]",
"/pet/findByTags": "[Object]",
"/pet/{petId}": "[Object]",
"/pet/{petId}/uploadImage": "[Object]",
"/store/inventory": "[Object]",
"/store/order": "[Object]",
"/store/order/{orderId}": "[Object]",
"/user": "[Object]",
"/user/createWithArray": "[Object]",
"/user/createWithList": "[Object]",
"/user/login": "[Object]",
"/user/logout": "[Object]",
"/user/{username}": "[Object]"
},
"schemes": [
"https",
"http"
],
"securityDefinitions": {
"api_key": "[Object]",
"petstore_auth": "[Object]"
},
"swagger": "2.0",
"tags": [
"[Object]",
"[Object]",
"[Object]"
]
},
"url": "http://petstore.swagger.io/v2/swagger.json"
}
关于javascript - 如何在React Native Sagger中请求数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63742246/
我是一名优秀的程序员,十分优秀!