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
193 views
in Technique[技术] by (71.8m points)

node.js - Why is expressjs POST request returning [object Object]?

app.post("/api/orders", async(request, response) => {
  try {
    await client.connect();
    console.log("Connected correctly to server");
    const db = client.db(dbName);
    // Use the collection
    console.log("search options " + json(request));
  const col = db.collection("Orders");      
  // const inserted = await col.insertOne();
   response.send("thanks")
   } catch (err) {
    console.log(err.stack);
}

 
});

/

let orderData = {subject: "subject", location: "location", price:"price", spaces: "spaces"};
const optionsPOST = {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
  },
  body: JSON.stringify(orderData)

};
console.log(JSON.stringify(orderData))
fetch("/api/orders", optionsPOST);

I am trying to post some data that then is added to mongodb atlas, however, when I post my data I get undefined.

question from:https://stackoverflow.com/questions/65945302/why-is-expressjs-post-request-returning-object-object

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

1 Reply

0 votes
by (71.8m points)

Try using a simple fetch request

fetch('/api/orders', optionsPOST)
  .then(response => response.json())
  .then(data => console.log(data));

This should also help: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch


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

...