返回

javascript-TypeError:无法读取未定义的属性(读取位置)在res.重定向中

发布时间:2022-04-23 11:45:59 270
# 前端

我有以下功能从数据库中检索对象并提取URL:

async redirect(id: string, redirectFunction: Function) {
    if (!IdExists(id)) {
      throw new Error(`ID ${id} does not exist.`);
    }
    const redirectLocation: string = await prisma.url.findUnique({
        where: { uniqueId: id },
        select: { url: true },
    }).then((data) => {
        return data?.url!;
    });

    redirectFunction('http://' + redirectLocation); 
}

在以下代码段中调用该函数:

app.get('/:id', async (req, res) => {
    try {
      redirectController.redirect(req.params.id, res.redirect);
    } catch (error) {
      console.error(error);
    } 
});

然而,我得到了TypeError: Cannot read properties of undefined (reading 'location'),我发现这个错误与res.redirect方法然而,当我用console.log为了进行调试,URL会正确显示。是什么导致了这个错误?

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像