I'm having some problem with my assignment on Node.js with mongoDB for a long time and can't figure out whats the problem. Didn't find any solution on here yet. Everything looks as it should be. I'm able to get/post/delete, but the 'put' operation keeps give me a null error for no apparent reason.
let moid;
async function loadData()
{
let params = new URLSearchParams (location.search)
moid = params.get("moid")
console.log(moid)
let resp = await fetch("http://localhost:8000/api/movies/" + moid)
let data = await resp.json()
document.getElementById("Name").value = data.Name;
document.getElementById("dir").value = data.Director;
document.getElementById("pry").value = data.PremieredYear;
}
async function Update()
{
let nameValue = document.getElementById("name").value;
let dirValue = document.getElementById ("dir").value;
let pryValue = document.getElementById ("pry").value;
let obj = {
Name: nameValue,
Director: dirValue,
PremieredYear: pryValue
};
let fetchParams = { method: 'PUT',
body: JSON.stringify(obj),
headers: {"Content-Type" : "application/json"}
}
let resp = await fetch ("http://localhost:8000/api/movies/" + moid, fetchParams)
let data = await resp.json ()
location.href = "AllData.html"
}
<body onload = "loadData()">
<div class=" container">
<h1 style="background-color: white; color: black; width: fit-content; font-size: xx-large; position: relative; right: 200px; top:7px;">Update Movie</h1>
<div class="form-group row">
<button type="button" class="btn btn-primary" style="position: relative; left: 10px">Name</button>
<div class="col-sm-10">
<input type="text" style="width: fit-content;" class="form-control form-control-sm" id="Name" placeholder="Enter Movie Name">
</div>
</div>
<div class="form-group row">
<button type="button" class="btn btn-primary" style="position: relative; left: 10px">Director</button>
<div class="col-sm-10">
<input type="text" style="width: fit-content;" class="form-control" id="dir" placeholder="Enter Name">
</div>
</div>
<div class="form-group row">
<button type="button" class="btn btn-primary" style="position: relative; left: 10px">Premiered Year</button>
<div class="col-sm-10">
<input type="text" style="width: fit-content;" class="form-control form-control-sm" id="pry" placeholder="Enter Premiered Year">
</div>
</div>
</div>
<div>
<button type="submit" class="btn btn-secondary" style="position: relative; left: 200px" onclick="Update()">Update</button>
<button type="submit" class="btn btn-secondary" style="position: relative; left: 210px" onclick="Delete()">Delete</button>
</div>
</body>