Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
86 views
in Technique[技术] by (71.8m points)

javascript - GET request response express.js

I'm Trying to send a GET req to my express server and logging it to the console using

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

but getting this(below) as the result and not the mongoose database array expected. what am i missing?

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

Here's the route:

router.route('/').get((req, res) => {
  variable.find()
    .then(variable => res.json(variable))
    .catch(err => res.status(400).json('Error: ' + err));
});```
question from:https://stackoverflow.com/questions/65880314/get-request-response-express-js

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you just have to use response.json() to get data

const get = document.getElementById("getData");
          get.addEventListener('click', getData)
          
    const myRequest = new Request('/url here/');
    
    async function getData () { 
    
    await fetch(request, {
          method: "GET"
        })
        .catch(() => {
          console.log("Fail zone");
        })
        .then((res) => {
          if (res.ok) {
            res.json().then((json) => {
             console.log(json)
            });
          } else {
            console.log("error", res);
          }
        });
      };

for reference you can check this here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...