简体   繁体   中英

Send a variable from my server to my client

when I console.log(response.data) it is undefined. Why is that?

I already tried res.send and res.json but they both just return undefined...

Should I be changing something in my client or in my server

Server side

//server js file
app.get("/updateCart", (req, res) => {
    //get items from cart
    dataServiceAuth.findUser("joe")
    .then((user) => {
      res.json(user[0]); //I know user[0] has a value I console.log it to the screen
    })
    .catch((err) => {
      console.log("ERROR. Can't find user")
    });
});

Client side

//client file
$("document").ready(function () {
fetch("/updateCart", {method: "GET"}).then(response => {
    console.log(response.data);
  });
});
app.get('/getItems' (req, res) => {
    let x = [5, 4, 3, 2, 1];
    res.send(x)
})

OR

app.get('/getItems' (req, res) => {
    let x = [5, 4, 3, 2, 1];
    res.json(x)
})

Client.js

var myItems;
fetch("/getItems", method: "GET").then(response => {
  myItems = response.data;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM