I am doing redis or what?
Tags: redishackathon, redis, node, watercooler
Published: 2022-08-21 03:16:17

First of all, let's start with the errors.

```sh
node:internal/process/esm_loader:94
    internalBinding('errors').triggerUncaughtException(
                              ^

[ErrorReply: ERR unknown command 'JSON.SET', with args beginning with: 'Album:01GAZ32CZWSPB78HE8M75VH1GR' '.' '{"artist":"Mushroomhead","title":"The Righteous & The Butterfly","year":2014,"genres":["m' ]
```

I have my `redis-server` running on the background and to see how things work with redis a primary database, I went to [redis-om-node](https://github.com/redis/redis-om-node). I put some snippets together trying to come to an understanding of the whole flow.

```js
import { Client, Entity, Schema, Repository } from "redis-om";

const client = new Client();
await client.open("redis://localhost:6379");

class Album extends Entity {}

const albumSchema = new Schema(Album, {
  artist: { type: "string" },
  title: { type: "text" },
  year: { type: "number" },
  genres: { type: "string[]" },
  outOfPublication: { type: "boolean" }
});

const albumRepository = client.fetchRepository(albumSchema);

const album = albumRepository.createEntity();
album.artist = "Mushroomhead";
album.title = "The Righteous & The Butterfly";
album.year = 2014;
album.genres = ["metal"];
album.outOfPublication = true;

const id = await albumRepository.save(album); // '01FJYWEYRHYFT8YTEGQBABJ43J'

console.log(id);
```

Anyone one who has skimmed through the [redis-om-node](https://github.com/redis/redis-om-node) or [redis-om](https://www.npmjs.com/package/redis-om/v/0.1.5) will recognize this code.

In `package.json` file, I have installed these packages since I am changing the primary database from _MongoDB_ to _Redis_.

```json
...
  "dependencies": {
    "bcrypt": "^5.0.0",
    "cors": "^2.8.5",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "joi": "^17.6.0",
    "joi-password": "^3.0.1",
    "jsonwebtoken": "^8.5.1",
    "morgan": "^1.10.0",
    "morgan-json": "^1.1.0",
    "redis": "^4.2.0",
    "redis-om": "^0.3.6",
    "winston": "^3.7.2"
  },
...
```

Getting an error and not knowing where it came from or what, is something I have dealt with for quite sometime now. I know I am not the oly person. So keep pushing.

I was thinking it was not me but the code or something so I started digging Google and Stackoverflow and dev.to. I found out that I could create a new repository instead of fetching it as I did or it has been done over here, `const albumRepository = client.fetchRepository(albumSchema);` I updated my code.

```js
import { Client, Entity, Schema, Repository } from "redis-om";

const client = new Client();
await client.open("redis://localhost:6379");

class Album extends Entity {}

const albumSchema = new Schema(
  Album,
  {
    artist: { type: "string" },
    title: { type: "text" },
    year: { type: "number" },
    genres: { type: "string[]" },
    outOfPublication: { type: "boolean" }
  },
  { dataStructure: "JSON" }
);

// const albumRepository = client.fetchRepository(albumSchema);
const repository = new Repository(client, albumSchema);

// const album = albumRepository.createEntity();
const album = repository.createEntity();
album.artist = "Mushroomhead";
album.title = "The Righteous & The Butterfly";
album.year = 2014;
album.genres = ["metal"];
album.outOfPublication = true;

// const id = await albumRepository.save(album);
const id = await repository.save(album);

console.log(id);
```

Guess what? There was an error which was different from the previous.

```sh
/home/user/Projects/web/ARMS-redis/node_modules/redis-om/dist/index.js:907
    const id = this.schema.generateId();
                           ^

TypeError: this.schema.generateId is not a function
    at Repository.createEntity (/home/user/Projects/web/ARMS-redis/node_modules/redis-om/dist/index.js:907:28)
    at file:///home/user/Projects/web/ARMS-redis/src/index.js:24:26
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
```

I went to `/home/user/Projects/web/ARMS-redis/node_modules/redis-om/dist/index.js:907`

```js
  createEntity(data = {}) {
    const id = this.schema.generateId();
    return new this.schema.entityCtor(this.schema, id, data);
  }
```

This is the `createEntity` that has or points to the `generateId` function that does not exist. There was a class, `var Schema = class { ... }` inside the same file and it has the `generateId` function.

I went back to digging and I found out that there is another method on the repository, `createAndSave({})`. [express-redis-om-workshop](https://github.com/redis-developer/express-redis-om-workshop/blob/solution/routers/person-router.js).

I made modifications:

```js
import { Client, Entity, Schema, Repository } from "redis-om";

const client = new Client();
await client.open("redis://localhost:6379");

class Album extends Entity {}

const albumSchema = new Schema(
  Album,
  {
    artist: { type: "string" },
    title: { type: "text" },
    year: { type: "number" },
    genres: { type: "string[]" },
    outOfPublication: { type: "boolean" }
  },
  { dataStructure: "JSON" }
);

// const albumRepository = client.fetchRepository(albumSchema);
const repository = new Repository(client, albumSchema);

// const album = albumRepository.createEntity();
// const album = repository.createEntity();
// album.artist = "Mushroomhead";
// album.title = "The Righteous & The Butterfly";
// album.year = 2014;
// album.genres = ["metal"];
// album.outOfPublication = true;

// const id = await albumRepository.save(album);
// const id = await repository.save(album);

const id = await repository.createAndSave({
  artist: "Mushroomhead",
  title: "The Righteous & The Butterfly",
  title: "The Righteous & The Butterfly",
  year: 2014,
  genres: ["metal"],
  outOfPublication: true
});

console.log(id);
```

and still,

```sh
/home/user/Projects/web/ARMS-redis/node_modules/redis-om/dist/index.js:907
    const id = this.schema.generateId();
                           ^

TypeError: this.schema.generateId is not a function
    at Repository.createEntity (/home/user/Projects/web/ARMS-redis/node_modules/redis-om/dist/index.js:907:28)
    at Repository.createAndSave (/home/user/Projects/web/ARMS-redis/node_modules/redis-om/dist/index.js:915:25)
    at file:///home/user/Projects/web/ARMS-redis/src/index.js:34:29
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
```

At this moment, I can feel the stress and tiredness on my skull (Ghost rider). I watched some anime and walk about a bit. First things first, I went back to the main article about the hackathon and I started glancing through bit by bit, looking for clues. [redis.io](https://redis.io/). Is there any different between the two, [redis.com](redis.com)? I don't know so I couldn't tell. [redis.io](https://redis.io/), I read, `The open source, in-memory data store used by millions of developers as a database, cache, streaming engine, and message broker.` and I remembered I have read it before or something like that. I click on _DOCS_ as any developer would and before I knew I was at, [redis-stack](https://redis.io/docs/stack/). I realized, _redis-stack_ is not just the same as _redis_ but `Extends Redis with modern data models and processing engines. Includes documentation for the bundled Redis modules and RedisInsight.`

To conclude, I wasted the whole day looking in the wrong direction, doing the right thing. I installed _redis-stack_ and used this solution to fix another, [Could not create server TCP listening socket \*:6379](https://stackoverflow.com/questions/32947076/redis-server-in-ubuntu14-04-bind-address-already-in-use).

I am going to sleep as a result. I think I will do well tomorrow.