[MUSIC] Right. So, we've tidied up our code now, but it's also time to confess that there's some problems with the code. There are two bugs I'd like to fix. And let me just show you what those bugs are, so it's really clear what we need to do. So, first bug is this one. Right. So, if I'm logged in and I load the page in here. You can see in my documents list, there isn't anything there. And if I start typing you can see that the reactivity is not really working. Now, it's okay if I create a new document. That's okay, and load that over there and make it so that it's public. You can see I can easily load it over here no problem. So, once I've got a public document, everything's up and running, but that boot strapping thing that creates the first document on the startup is not quite working properly because if we look in the database here, there is our old friend. Meteor mongo. And if I go db.documents find pretty. So, you can see actually, two entries in there. So that's the one, the first one is the one that's created by the service start up script. And the second one is the one that I just made which is that sort of public one. So, what's the problem? Well, it looks like not both of them are appearing in the list. And what I'd like to happen is every time I start the app up everyone comes and sees the first document to start off with. And so it's very obvious maybe the problem is to do with is private because that's actually the main difference between the two documents. This one hasn't got an is private field. And remember we're using that is private field in the publication where we sort of show them which documents they can see. So, what I'm going to do is I'm going to tweak the filter so that it works properly, and so I'm going to go into my, so the filter is defined in the server. And it's here. So there's the filter, so it's basically saying if isPrivate is false or owner is the current user. So, either of those is true. The problem is, the document doesn't have it as a private field, so I can't test on it. Well, actually I can, I can do a tricky bit of sort of double logic here. If I do not equals to true. Then that's okay, right? So, that's a slight change there. So, imagine is private is not there, then is private will come up as undefined. So, then it will compare undefined to true. Undefined is not true, it's different. So that's good, so it will match that filter. But if it is set, if private is set and it's equal to false. In other words, it's not private, it's public, then again it will match the filter and it will be fine. So, let's just save that and see if the document now appears in the list. Okay, there we have it. So, when I've got two documents in the list, okay so jump between them and obviously I'm not the owner of this first one, so I can't change the settings on it, which is how it should operate. But, then there's another problem. Can you see as I'm jumping between the documents, I can see my second bug happening there, which is that instead of editing users, it's not changing. When I go to another document. So, for some reason, it's not understanding that I'm moving to a new document. And let's just take a step back and remember what we did earlier in the course, by looking at this visualization. So, here we have the idea of multiple actors all looking at one document, and that's how we started it. And remember, we sort of fixed the code so then you had multiple documents with possibly multiple people editing different documents. And we did that by remembering in the session which you can see there that each person is remembering in their own session which one they're editing. And we made that work with the document editing thing. But, what we didn't do is we didn't propagate that change into the editing users functionality. So, that's all I need to do. I need to go and check that functionality. So, there are two parts to it. The bit where I sort of update the list of editing users, and the bit where I display the list of editing users. So let's fix the update first, and that is in a method. So I go into my server, so in my shared code, cause methods are viewed by both server and client. And I look for the appropriate method, which is addEditingUser. And the first thing I spot is that actually, we're not pulling out the correct document. We're pulling out just the first document that comes out. All right? So, I need some sort of filter there that says, well, I want to load the document that I'm currently looking at. So, the problem is in a method, we might not necessarily have access to the sessions. So, remember we're storing the ID on the session, but we might not have access in the method. Cause when it runs on the server, when it actually runs on the proper server, I don't have access to the session cause that's sitting in the browser, okay. So, I need to some how pass in a document ID, so I simply add a perimeter to my method and then put a filter in which uses that parameter to filter the document. So, that means it will only find the correct document. But, I need to pass that parameter in and I do that in the client code where I am calling the method. Client Main, and it's down here. Where I call get add, there we go, Meteor.call("addEditingUser"). So remember, the callback is in whenever they make an edit on the document, it makes sure that they're in that list of users editing the document. Meteor call, and I simply just pass it a parameter like this. Session get docid, okay? So, now I'm passing that in, and I can, if I make sure I saved all my files, and I should now, if I look in the database, see that I'm getting sort of more than one entry. So, I've now got three entries so if I then create a new document again an start editing, see I've now got four entries. And I can even look at those. So, now that's my last one. So, you can see myk has been registered as editing a document, so that means that I'm, and I've got separate sets for the other documents. That's good, so I'm now correctly maintaining a set of users for each document in the database. And the other step, the final step is to then display that correctly. In the browser so I go back to the client code and find the bit where I'm accessing the users which is right here template editing users helpers and again I see the same problem which is that I'm just pulling out the first document I can find instead of pulling out the actual document I'm editing so we just drop in the same filter there so it's ID is equal to session get idea. Remember I've got access to the session in this code because I'm actually on my client. This is the client code. The client has access to the session. So, you need to kinda maintain this awareness of what kind of scoping is going on in the variables. Remember we talked about scoping in a previous lesson. So, hopefully that work now as I move around. Between different documents. So, I created a new document there and it jumped into a new document. Until I start editing my name doesn't appear and if I make that one so it's public then I jump over to here. Now, let's just give it a name so I can find it. New public doc, okay. So, that should appear in the list over here. New public doc. Right, there we go. Soon as I go into it you can see my name's appeared on the list of editing users, and if I jump to a different document it should have different users, depending on whose editing it. Right, so that's all working now. I fixed two bugs there, I fixed the start up bug where we weren't starting up with the same document when we logged in. And I fixed the editing users bug where the code was assuming there was only one document, and I fixed it so it can now deal with multiple documents and using the session to remember which one it should be. [MUSIC]