【问题标题】:List request not working (Mongoose in Node.js with Typescript)列表请求不起作用(带有 Typescript 的 Node.js 中的 Mongoose)
【发布时间】:2020-09-21 22:42:27
【问题描述】:

我在 Nodejs 中使用带有 typescript 的猫鼬。在列表请求中,我有以下代码:

async list(req: Request, res: Response, next: NextFunction)
{
    try {
        let list: ListInterface[] = []
        await RequestModel.find().then(requests =>
        {
            requests.map(async request =>
            {
                const client = await Client.findById(request.cliente)
                const seller = await Seller.findById(request.vendedor)
                const company = await Company.findById(request.representada)
                const tmp =
                {
                    id: request._id,
                    data: request.data,
                    cliente: String(client?.nome_fantasia),
                    vendedor: String(seller?.nome),
                    representada: String(company?.nome_fantasia),
                    tipo: request.tipo,
                    status: request.status
                }
                list.push(tmp)
                console.log(tmp)
            })
        })
        console.log(list)
        return res.json(list)
    } catch (error) {
        next(error)
    }
}

当我在 Insomnia 中提出请求时,我收到一个空数组(在终端中——因为console.log(list)——和在 Insomnia 的预览中):[]。在终端中,由于console.log(tmp) 命令,我收到了正确的数据。

我已经尝试将列表声明为const list = request.map(...) 之类的东西,但它在终端中给了我[Promise<Pending>, Promise<Pending>],在Insomnia 中给了我[{}, {}]。我无法重复此测试的完全相同的代码,但我记得那些结果。

我什至不确定我在滥用哪种技术。有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: node.js typescript mongoose


    【解决方案1】:

    .map函数返回一个promise列表,所以在发回响应之前必须等待它们。

    async list(req: Request, res: Response, next: NextFunction)
    {
        try {
            let list: ListInterface[] = []
            const requests = await RequestModel.find()
            
            // collect all promises from the loop
            const promises = requests.map(async request =>
            {
                const client = await Client.findById(request.cliente)
                const seller = await Seller.findById(request.vendedor)
                const company = await Company.findById(request.representada)
                const tmp =
                {
                    id: request._id,
                    data: request.data,
                    cliente: String(client?.nome_fantasia),
                    vendedor: String(seller?.nome),
                    representada: String(company?.nome_fantasia),
                    tipo: request.tipo,
                    status: request.status
                }
                list.push(tmp)
                console.log(tmp)
            })
            
            // wait for all promises to complete
            await Promise.all(promises)
            
            console.log(list)
            return res.json(list)
        } catch (error) {
            next(error)
        }
    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-10
      • 1970-01-01
      • 1970-01-01
      • 2014-07-20
      • 2018-08-06
      相关资源
      最近更新 更多