(redis-om & fastify) this.writeEntity is not a function

Viewed 224

I'm learning to use the Redis for my backend database and I would like to try using redis-om for fastify not sure whether they are compatible or not, but I got error.

I use service of app.redislabs.com

I don't know what I just messed up? And how can I fix the problem?

server.js

const { createCar, createIndex } = require("./redis");

app.post("/add", async (req, res) => {
  await createIndex();
  const { make, model, image, description } = req.body;
  const data = { make, model, image, description };
  await createCar(data);
  res.code(200).send('ok');
});
const PORT = 5000;
app.listen(PORT, function (err) {
  if (err) {
    app.log.error(err);
    process.exit(1);
  }
});

redis.js

const { Client, Entity, Schema, Repository } = require("redis-om");

const client = new Client();

const connect = async () => {
    if (!client.isOpen()) {
        await client.open("redis://default:password@localhost:6379");
    } else {
        console.log("CONNECTED");
    }
};

class Car extends Entity {}
let schema = new Schema(
    Car,
    {
        make: { type: "string" },
        model: { type: "string" },
        image: { type: "string" },
        description: { type: "string" },
    },
    { dataStructure: "JSON" }
);

const createCar = async (data) => {
    await connect();

    const repository = new Repository(schema, client);
    const car = repository.createEntity(data);

    const id = await repository.save(car);

    return id;
};
const createIndex = async () => {
    await connect();

    const repository = new Repository(schema, client);
    await repository.createIndex();
};
module.exports = {
    createCar,
    createIndex,
};

My JSON Body

1 Answers

You cannot call new on Repository. This is a breaking change I introduced in version 0.2.0 of Redis OM. There are a couple of others that are documented in the CHANGELOG.

Call const repository = client.fetchRepository(schema) instead, as shown here. Unfortunately, there are some videos and blogs that have the older syntax and so this crops up from time to time.

Thanks for using my library!

Related