如何解决GET 请求响应 express.js
我正在尝试将 GET 请求发送到我的快速服务器并使用
将其记录到控制台const get = document.getElementById("getData");
get.addEventListener('click',getData)
const myRequest = new Request('/url here/',{
method: 'GET',});
function getData () { fetch(myRequest)
.then(response => console.log(response))
};
但是得到这个(下面)作为结果而不是预期的猫鼬数据库数组。我错过了什么?
Response {type: "basic",url: "url here",redirected: false,status: 200,ok: true, …}body: (...)bodyUsed: falseheaders: Headers {}ok: trueredirected: falsestatus: 200statusText: ""type: "basic"url: "url here/"__proto__: ResponsearrayBuffer: ƒ arrayBuffer()blob: ƒ blob()arguments: (...)caller: (...)length: 0name: "blob"__proto__: ƒ ()[[Scopes]]: Scopes[0]body: (...)bodyUsed: (...)clone: ƒ clone()formData: ƒ formData()headers: (...)json: ƒ json()ok: (...)redirected: (...)status: (...)statusText: (...)text: ƒ text()type: (...)url: (...)constructor: ƒ Response()Symbol(Symbol.toStringTag): "Response"get body: ƒ body()get bodyUsed: ƒ bodyUsed()get headers: ƒ headers()get ok: ƒ ok()get redirected: ƒ redirected()get status: ƒ status()get statusText: ƒ statusText()get type: ƒ type()get url: ƒ url()__proto__: Object
路线如下:
router.route('/').get((req,res) => {
variable.find()
.then(variable => res.json(variable))
.catch(err => res.status(400).json('Error: ' + err));
});```
解决方法
你只需要使用 response.json() 来获取数据
#include <iostream>
#include <cstdlib>
#include <windows.h>
// Moves cursor by (DeltaX,DeltaY) = (offx,offy) relative to current position.
inline bool WinMoveCursor(int offx,int offy) {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO cbsi = {};
if (!GetConsoleScreenBufferInfo(hConsole,&cbsi))
return false;
COORD coord = cbsi.dwCursorPosition;
coord.X += offx;
coord.Y += offy;
if (!SetConsoleCursorPosition(hConsole,coord))
return false;
return true;
}
static void ClearLines(size_t cnt,size_t width = 40) { // Clears cnt last lines.
std::cout << "\r";
for (size_t i = 0; i < cnt; ++i) {
for (size_t j = 0; j < width; ++j)
std::cout << " ";
std::cout << "\r";
if (i + 1 < cnt)
WinMoveCursor(0,-1); // Move cursor one line up,WinAPI.
}
}
int main() {
SetConsoleOutputCP(65001); // Enable UTF-8 if needed.
bool good = false;
char buy = 0;
std::cout << "Enter number:" << std::endl;
while (!good) {
std::cin >> buy;
switch (buy) {
case '1': {
std::cout << "Valid number." << std::endl;
good = true;
break;
}
default: {
std::cout << "Sorry thats not a valid number!\n";
std::system("pause");
ClearLines(4);
break;
}
}
}
}
作为参考,您可以查看此here
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。