我的意图是提出请求方法:“ POST” 。 这是我的应用程序:
.addEventListener("click", async function() {
var newTaskName = document.getElementById("inNewTask").value.trim();
let newTask = { title: newTaskName, sLine: 1 };
const url = "http://localhost:3000/tasks/";
try {
const response = await fetch(url, {
method: "POST",
body: newTask,
headers: {
"Content-Type": "application/json"
},
mode: "no-cors"
});
console.log(newTask);
console.log(response);
return await response;
} catch (error) {
console.error("Error:", error);
}
addTaskToHtml(newTask);
});
这是我的服务器上的解决方案:
router.post("/", (req, res) => {
mongoose
.connect("mongodb://localhost:27017/kanban", {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(connection => {
let task = new Task({
title: req.body.title,
// tag: req.body.tag,
sLine: req.body.sLine
});
task
.save()
.then(task => {
res.send(task);
console.log("Task is there");
})
.catch(err => {
console.log("Something gets wrong");
console.error(err.message);
})
.then(task => {
connection.disconnect();
res.send(task);
});
})
.catch(err => {
res.status(400).send({ err: "there is no database" });
});
});
我尝试了许多解决方案,可能我错过了重要的意义。
我的服务器最终看不到我的标题,并且经常发回未定义的消息。
没有到达到数据库。