This lesson will demonstrate the creation of a document within the Domino Database by using the Domino Data Services and the NodeJS application.
It highlights that a successful submission to Domino using the Domino Data Services returns a HTTP header with the location of the created document.
Instructor: [00:00] 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.
[00:23] 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.
[00:36] 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.
[00:47] 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.
[01:07] 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.
[01:23] 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.
[01:40] 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.
[02:19] 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.
[02:47] 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.
[03:05] 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.
[03:19] 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.
[03:35] 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.
[04:03] 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.
[04:28] 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.
[04:52] 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.
[05:14] 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.
[05:33] 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.
[05:49] Let's just see what they looks like in the browser itself. I've started my server, refreshed my browser.
[05:55] 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.
[06:18] The result of that is the JavaScript is now showing my Star Wars Record input form. If I give it a name, some height information, I'm going to make this one male. I'm going to have two films selected. I hit save. Remember this has gone off to the Node server to Domino, done the post, created the document, and then gone via the location and got the Domino data itself.
[06:45] 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.
[06:58] 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.