(redis-om & fastify) this.writeEntity 不是一个函数

Jok*_*ght 2 redis fastify

我正在学习使用 Redis 作为我的后端数据库,我想尝试使用redis-om不确定fastify它们是否兼容,但出现错误。

我使用 app.redislabs.com 的服务

我不知道我刚刚搞砸了什么?我该如何解决这个问题?

服务器.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);
  }
});
Run Code Online (Sandbox Code Playgroud)

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,
};
Run Code Online (Sandbox Code Playgroud)

我的 JSON 正文

Guy*_*yse 8

你不能new打电话Repository。这是我在 Redis OM 0.2.0 版本中引入的一项重大更改。CHANGELOG中记录了其他一些内容。

const repository = client.fetchRepository(schema)改为调用,如此处所示。不幸的是,有些视频和博客具有较旧的语法,因此这种情况时不时就会出现。

感谢您使用我的图书馆!