gpt4 book ai didi

javascript - 如何在React Native Sagger中请求数据

转载 作者:行者123 更新时间:2023-12-01 16:16:19 25 4
gpt4 key购买 nike

我正在使用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)
此后,没有在线可用的教程。我只是尝试了一些代码,但它不起作用。
如果我的代码是错误的,或者如果我缺少某些东西,请纠正我。
如何从 petstore请求数据?

最佳答案

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);
});
});

我如何找到正确的函数名称
我同意这有点令人困惑,因为swagger API确实列出了
GET请求 /pet​/findByStatus和函数名称与此不符。
我通过记录来自 Swagger(...)的响应(在您的示例中称为 client)并查看此对象具有的属性和值来找到正确的函数名称:
Swagger('http://petstore.swagger.io/v2/swagger.json').then((client) => {
console.log(client)
});

关于与您的问题有关的 promise 的说明
在文档中,我们可以看到调用 Swagger(url, options)返回 Promise( https://www.npmjs.com/package/swagger-react-native-client#constructor-and-methods)。

Promise对象表示异步操作的最终完成(或失败)及其结果值。

资料来源: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
因此,这意味着在 then子句中,我们可以访问(成功)异步操作的结果值,即 Swagger(...)调用。在示例中,您已将此结果称为 client
这个结果是一个对象,其中包含许多不同的数据,但重要的是要回答您的问题;它具有一个名为 apis的属性,该属性具有一个对象作为值。该对象包含您可以使用的所有与api相关的功能。
从Swagger Promise返回的对象的表示形式
这是在示例中记录 [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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com