TypeORM和MongoDB以及Repositories:无法读取未定义的属性'prototype'

4
我正在尝试使用存储库将TypeORM与MongoDB集成。然而,当我试图使用存储库管理数据库时,使用与此仓库相同的结构,事情有些走样。我遇到了以下错误:
未处理的Promise拒绝警告: 类型错误: 无法读取undefined的原型
我尝试了以下代码:
import { Request, Response } from 'express';
import { getMongoRepository } from "typeorm";
import Task from "../models/Task";

export default class TasksController {
async listAll(request: Request, response: Response): Promise<Response> {
    const tasksRepository = getMongoRepository(Task);
    try {
      const tasks = await tasksRepository.find();
      return response.status(200).json({ "items": tasks });
    } catch (err) {
      return response.status(400).json({
        message: err.message,
      });
    }
  }
}

我知道这个错误是关于实现.find()方法的。甚至我已经成功获取了数据,使用了这篇帖子中的建议,将以下内容替换为:

const tasks = await tasksRepository.find();

使用

const tasks = await tasksRepository.createCursor(tasksRepository.find()).toArray();

但我仍然遇到了上述错误。

有谁能理解发生了什么?

我也成功地通过以下脚本直接将数据保存到数据库中:

server.ts

import express from 'express';
import { createConnection } from 'typeorm'

const app = express();
const port = 3333;
createConnection();

app.use(express.json());

app.post('/tasks', (async (request, response) => {
  const { item } = request.body;
  task.item = item;

  const task = new Task();
  (await connection).mongoManager.save(task);

  return response.send(task);
}))

app.listen(port, () =>
  console.log(`Server running on port ${port}`)
);
2个回答

16

这应该是正确的答案。 - Victor Ian
追踪:https://github.com/typeorm/typeorm/issues/8146 - Corky3892
这意味着像$lookup这样的东西将不可能,我只是在问而已,想知道如果我要降级还是一次性切换到mongoose。 - Vixson
是的,将MongoDB降级以使用TypeORM并不是一个可行的解决方案。Mongoose可以完成工作,但显然缺少TypeORM提供的一些TS本地特性。基本上,需要更新TypeORM以支持4.2版本。 - Corky3892

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接