Javascript 如何在react native swagger中请求数据
我正在使用并试图从中请求数据 我按照教程做了这件事Javascript 如何在react native swagger中请求数据,javascript,react-native,swagger,Javascript,React Native,Swagger,我正在使用并试图从中请求数据 我按照教程做了这件事 Swagger('https://petstore.swagger.io/v2/swagger.json') .then((client) => { console.log(client); console.log('---client---'); client.apis.pet .findByStatus({status: 'available'}) // client
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请求数据?TL;博士 我认为您正在寻找函数
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确实列出了 获取请求
/pet/findByStatus
,函数名与此不对应
通过记录示例中名为client
的Swagger(…)
的响应,并查看此对象的属性和值,我找到了正确的函数名:
Swagger('http://petstore.swagger.io/v2/swagger.json').then((client) => {
console.log(client)
});
关于与您的问题相关的承诺的解释 在文档中,我们可以看到调用
Swagger(url,options)
返回一个Promise
()
Promise对象表示异步操作的最终完成(或失败)及其结果值
资料来源:
这意味着在then
子句中,我们可以访问(成功的)异步操作的结果值,即Swagger(…)
调用。在您的示例中,您调用了这个结果client
这个结果是一个包含很多不同数据的对象,但对于你的问题来说很重要;它有一个名为api
的属性,该属性将对象作为值。此对象包含您可以使用的所有api相关函数
从Swagger Promise返回的对象的表示
下面是一个高度格式化且稍加调整的(在[Object]
和[Function anonymous]
实例中添加引号)表示您在示例中登录客户端时得到的结果:
{
"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"
}
您可以只显示一些关于客户端对象的外观以及如何找到它的代码行吗。不管怎样,我得到了答案,我把它标为答案。但是,根据你的答案,我花了一些时间来解决这个问题。@rock我已经更新了我的答案并添加了更多的解释。我已经检查过了。太好了。