Creating a job board with Node.js and FaunaDB - Part 3
Tags: fauna, javascript, node
Published: 2020-12-09 12:05:03

This is the third part of a series of posts documenting my studies with FaunaDB, these are the links for the first two posts:

{% post https://dev.to/ciochetta/creating-a-job-board-with-node-js-and-faunadb-part-1-51fb %}

{% post https://dev.to/ciochetta/creating-a-job-board-with-node-js-and-faunadb-part-2-1926 %}

This is the first post I am writting first in english and then translating to portuguese, let's see how this works out 

---

### Content

*[Company Module](#c-1): 
The hopeful start where I create this module and do some changes so it fits the model

*[Testing the module](#c-2):
Testing the two first functions of the module

*[Losing my sanity with the update function](#c-3):
It took me about an hour to write this part of the article because I got it wrong so many times, but I finally finished the update function



---

### Company Module <a name="c-1"></a>

I figured out that the company module should be really easy to do as well, so I will create it quickly and move on

I've started out by cloning the entire module for skills, then going to the validation, formatting and update and added the other fields.

In validation, I've added email:

![](https://dev-to-uploads.s3.amazonaws.com/i/hqgkx020d2i6c08gix0x.png)

And in the return, I've added each field:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/momrebgcx0isvfkykch9.png)

This "social_media" feels a little bit unsecure, I don't know how it will work out.

In the formatting, I've added the new fields:

![](https://dev-to-uploads.s3.amazonaws.com/i/e4mfn6pvc46qlv6kafh9.png)

In the update, I am sending now the entire object (which I am not sure it's a good idea, but let's find out: 

![](https://dev-to-uploads.s3.amazonaws.com/i/n8ntoxskj0ghzfxhm73m.png)

### Testing the module <a name="c-2"></a>

So now, I will add this companies module to the application and do some tests.

The url will be http://localhost/companies.

In my first try, I've realized the names were all incorrect in the routes.js file, so I've fixed it and tried again. 

List is working: 

![](https://dev-to-uploads.s3.amazonaws.com/i/lxg8bxmz3s0cvbmi2v80.png)

Insert is working: 

![](https://dev-to-uploads.s3.amazonaws.com/i/zmz8arkkyi2xl0kh2fia.png)

### Losing my sanity with the update function <a name="c-3"></a>

Update is not working, and that is about this "toString()" I am using in validation and also about not getting the value from the database before trying to update.

So I will first conditionally add these values to the object.

![](https://dev-to-uploads.s3.amazonaws.com/i/uxg9fvzoocfjunvh2jw4.png)

And I will add a part to this process, where I first get the company from the database, edit what I need, and then validate the object.

So using the query described [here](https://docs.fauna.com/fauna/current/api/fql/functions/get?lang=javascript) I will get the company.

I created this function to get it:

![](https://dev-to-uploads.s3.amazonaws.com/i/b6y0nxgd1ooyx3wpjnwu.png)

And validate it exists:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/kyrj0nsloj3g10g6884s.png)

And now, I will create a function that will transfer the fields from the request company to the database company.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/chlekvtbbygoepwg8mam.png)
So, this one is kinda weirda, I've copied it from a project I've done a time ago;

In the first part I grab the dbCompany object and break it down to it's entries, and then map those entries, changing any value that is present in the request for the new one.

After that, I find every key that is missing from the original object.

Then, I add each new key to the first array and create a new object from it.

This object will be the one I am validating.

So I will test again now and see how it goes.

And I messed something up, because I did edit it, however the data now looks like this:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/m68qul12gp9xqlyz7rss.png)

I think I messed up the validation function, I was doing 

```
return {
		Valid: true,
		Company : Company,
	};
```

And I think the correct is 

```
return {
		Valid: true,
		Company ,
	};
```
 
Tried again, failed again.

But after some logging, I think I figured it out, I think it's the data I am sending to Fauna, it should not be inside curly brackets.

So I tried again with the company without curly brackets and it worked.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/i0066x4duxkjl4tqhaz5.png)

### Conclusion

Never underestimate the amount of bugs that can show up just because you have done this kind of function before.

The company module works, so I will start with the Candidate module in the next post.

Repository for this project:

{% github https://github.com/ciochetta/faunajobboard no-readme %}