javascript - 如何在本机招摇中请求数据
问题描述
我正在使用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;博士
我认为您正在寻找功能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)
});
关于与您的问题相关的承诺的解释
在文档中,我们可以看到 CallingSwagger(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"
}
推荐阅读
- python - CV2 的 detectMultiScale 方法返回什么?
- android - 如果 Firestore 中的文档大小超过 1MB,会发生什么情况?
- reactjs - 使用 react-google-recaptcha-v3 库
- css - 如何让 css-purge 按我写的顺序处理我的 css 文件
- c# - 加载然后读取xml?
- r - gsub - 从单词末尾修剪一系列字母/数字
- php - laravel 5.7 cURL 错误 60:SSL 证书问题:无法获取本地颁发者证书
- multilingual - 没有“商店代码到网址”的多网站和多商店视图 magento 2.3
- java - 如何使用 sdk 27 在 android studio 中显示列表联系人电话
- javascript - 最小,最大长度和显示提交按钮不适用于小键盘