Instructor: To update data in Domino from Node.js, I recommend you modify your Internet site document and make sure all the available HTTP method unit are set. Make sure put, delete, and patch are also selected. We're going to show how to use the Node server and the Domino REST API to create some documents. Remember, we're still going to be using proper Domino security.
I've created a very basic form called "Star Wars" Record. We're going to capture name, height, gender which is a radio button, and a multi-value films. I have a record ready for Luke. That looks like that.
The other thing you need to make sure is the database has the correct setting for dealing with the REST API. At the bottom of the database properties, you've got allow Domino data service of views and documents.
The first thing we're going to do is modify our server.js file inside our Node application. We need to add a root which we're going to post to from our client application which in turn will then post through to Domino. We're going to call this root SWrecord for Star Wars Record.
Because we're using Domino security, we do need our Node DOM off sess ID to be sent to us. We'll deconstruct that from the request header. If we don't have it, then we're going to bounce it straight back to the client with a 401 status.
Next, we start sorting our options we're going to use on our request to Domino. Let's start with the URL. We're using the Domino REST API. It uses the document's collection as part of the API to do a post to.
You also have to associate the post with a form. In our case, it's going to be the Star Wars_Record form. We need to pass our security cookie. The body contents will be the same as our request body. We will just pass that directly through. It's a JSON request, so that's going to be true. We're resolved with full response again. We can see the full headers.
Next, we'll make our request to Domino. It's going to be a post. We're going to pass it the options. Then we're going to handle hopefully a successful result. The response back from Domino will either be an authentication failure or some error but also, if it's a success, it actually returns a location HTTP header. The body of the response actually contains no data at all.
What we're going to do is deconstruct either the authentication failure or the location header if everything's gone correctly from our response headers. If we've got an authentication failure, we're going to bat that straight back to the client with a status of 401.
Then the next thing I want to do is I don't want to return just a location of the saved document. I want to return the actual data that Domino has saved. Therefore, I'm going to do a second request now to Domino, a get request, to actually get the data.
We're going to set a new options object up for the new redirection. The URL or URI in this instance is the location header. We still need to do the security. We need to pass that.
We'll set JSON to true. Again, we'll resolve the full response. We're going to make this second request to Domino. It's a get. Hopefully we'll get a successful response. This time the content's the response we will send straight back to the browser. What actually we want is the body.
Now we've got to deal with any errors. The Domino REST API returns a JSON object with description of the error messages. We'll now catch those and send them back to the browser. We can set our status code based on the status codes returned from the Domino REST API error. Then we can send the actual error message itself which is a property of the error object. That's the inner request for the get.
Now we're going to do the outer request. But it's going to be exactly the same. That's the server.js file done. Now we can go ahead and test it. Our web page is a standard bootstrap page with a container. We've got a login form first which we use to log into Domino via the Node server. We need that to get our Node DOM sess ID.
Then we have another form which we use to collect the data for our Star Wars Record which includes name, height, radio buttons for the various gender, and then the films themselves which are checkboxes. Then we have a save button. Within our save function, we have a post to our SW Record root.
We got the Node DOM off sess ID from the local storage. We get hold of the form data. If you look at the film's one specifically, that has that syntax here for getting multiple values because checkboxes are multiple values. Let me just do like that, so you can see it.
Then we have Axios. We'll do a post to our Node server, passing the information. Then the result is outputted in a JSON stringified result, so we can see the data coming back from Domino. If it's an error, we catch it and show it in the error message.
Let's just see what they looks like in the browser itself. I've started my server, refreshed my browser.
The first thing I'm going to be presented with is my Domino login form. I enter my username and then my password. The response comes back from the Node server. We can see that in our network tab here. We'll just bring that up. We can see the response comes back with a Node DOM sess ID. We store that in local storage here.
Here is response back from Domino. As we can see, we've got a unit and Node ID and the fields I sent through and films, which was a multi-value field, has gone through as the array. See that in the notes database.
Opening my demo notes database, I see there a new document Luke two which I've just created. I open the document up. I have Luke two, height two, three, four, male, and two values in the films. That's worked successfully.