Hello,I have a question:My Express app routes "http://localhost:8000/" to my index.html, this is a static file, I am not using a template engine. This index.html has a normal JavaScript-file (called frontend.js) attached to it, to interact with the DOM. In the frontend.js there is a GET request using the Fetch api to the URL "/get-list", like this:fetch("/get-list", { method: "GET", headers: { "Content-Type": "application/text" } }).then(response => { response.text(); }).then(data => { console.log("Data:", data); }); In my Express app I have a GET route setup for this fetch, like this:const titles = []; app.get("/get-list", (req, res) => { Book.find() .then((result) => { res.send(result); console.log(result); }) .catch ((err) => { console.log(err); }); }); The Book instance represents a mongoose Model. As you can see the "/get-list" route searches through my database and stores the result in the result variable. The console.log(result); gives the expected result. But when I try to retrieve this data in my frontend.js with the fetch API it returns "undefined" in the console.log("Data:", data);So my actual question is: how do I send data from my database in Express to the frontend using the fetch API GET request?
Submitted September 05, 2020 at 05:50PM by rubennebur
No comments:
Post a Comment