We’ll cover posting new data to the server using fetch and the POST http method. We’ll also update the UI with a success message once the save has completed successfully.
[00:00] Our application is set up to load todos from a todos endpoint provided by JSON server. Let's update this so we can save new todos to the server as well. In todo-service.js, I'm going to export a new function which we'll call create todo. This is going to accept a new todo. Then we'll use fetch to post that to the server.
[00:23] Just like in the load todos function above, we're going to call fetch with our baseURL and return the resulting promise. I'm start with a return statement and a call to fetch passing in baseURL. By default, fetch will issue a get request. In order to post to the server, we'll need to pass in some options.
[00:40] After baseURL, I'm going to put in a second argument here that's going to be an object with all of our options. We'll start by defining the method, which is going to be post. Then we're going to need a couple of headers. I'm going to paste those in. We have an accept header for application/json and also a Content-Type header.
[00:59] Finally, we have to define the body of our post or the content that we want saved to the server. We'll define a body property here. We need to stringify our todo object. We're going to call JSON.stringify and pass in our todo. Like we did above, I'm going to call then, take the response, and call the JSON method on it. Now I can save that. I'm backing up that .js.
[01:25] I'm going to update my import to also include the create todo function that we created. Now that we have that, I'm going to come down to the handle submit method. We're adding our todo and updating our state. Now, I want to call create todo. I want to pass that new todo to the server. So we can confirm that this works, I'm going to add .then.
[01:51] When I get a response back, I'm going log out to the console todo added. I'll save this. Our browser will reload. I'll open up DevTools. Now, when I add a new todo, we'll see that our log shows todo added.
[02:10] If I do a full page reload, it will fetch our todos from the server, and it will include that new item that was just added. If we look at db.json, we'll see that we have this new item added with our generated ID, our name of new todo. Our default is complete value.
When I added
.then(() => console.log('todo added')) to
App.js, this is what I got in the browser console:
localhost/:1 Uncaught (in promise) TypeError: Failed to fetch
And this is what I got in the iTerm2 console when I ran npm test:
(node:40332) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Network request failed
(node:40332) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
I was able to add a new todo, but it did not persist since my network request failed.
And BTW, I am on
Oh and also, when I ran npm test, no error showed up in the actual code. Only the deprecation warning error:
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 0.886s, estimated 1s
What's interesting tho, before adding fetch, all the tests passed and showed up in the iTerm console as passing. Now only one test passes. But no errors are showing up.
And this is my code on Github: https://github.com/interglobalmedia/todo-list-create-react-app
Are you running the
json-server? It was covered in one of the videos, but the readme on the course repo has some instructions for setting it up.
This project relies on a local mocked server since building a true API layer with data persistence would have been far too out of scope and would have likely tripled the length of the course.
What you're seeing with the tests is likely related to the way Jest caches tests. It will try to run tests only where code has changed. When Jest is in watch mode, you can hit
a on your keyboard in the terminal to get it to run all of the tests. The code with the
fetch calls wasn't included in the tests, so it shouldn't be a factor there.
Hope this helps, and if you run into issues after setting up the server and getting a full test run, let me know and I'll try to help.
Thanks Andrew! It didn't work initially when I tried to use both npm start in one window and json-server in the other, but now I am using 7.10.0 and it worked when I tried it again. Not saying one had to do with the other, but it has been rectified. Thanks!
I have trouble posting new todo to db. It just saves the id and name and isComplete not saved. After refreshing the page, I see blank todos.