首页 > 解决方案 > res.redirect 附加到当前 url 不打开该 url

问题描述

所以我的代码如下

router.get('/:id', async function (req, res){
    try {
        const id = req.params.id;
        const student = await firestore.collection('urls').doc(id);
        const data = await student.get();
        var updatedviews = data.data().views +1;
        var updatedloc = data.data().location;
        fetch(ip)
            .then(async function(response) {
            response.json().then(async jsonData => {
                updatedloc += jsonData.region_code;
                await student.update({
                    views: updatedviews,
                    location: updatedloc
                     })
                     
            });
            })
            .catch(function(error) { });
        
            if(!data.exists) {
                res.status(404).send('Url with the given ID not found');
            }else {
                return res.redirect(data.data().longurl);
        
            }
       
    } catch (error) {
        res.status(400).send(error.message);
    }

});

但它的作用是将我从数据库中获取的长 url 附加到当前 url,如 localhost:5000/{longurl}

我想要的是它应该只在浏览器窗口中打开 {longurl}

标签: node.jsfirebaseexpressgoogle-cloud-firestore

解决方案


推荐阅读