我创建了一个URL缩短器.我使用POST方法创建了一个短ID,并将其插入到数据库中,模式如下:ShortenID,url,visitedAt. POST One成功,数据库也在更新. 但此redirectToUrl方法返回的url未找到.我控制台记录了ShortenID,它正确地记录在日志(log)中. 奇怪的是,我得到了这个日志(log),该日志(log)末尾的Clog(ShortenID)为空.

我也在遵循MVC模式.我还在学习 node ,以防这个信息会有什么不同

{
  _id: new ObjectId('657eaf900cd00ca7d2bc1f4d'),
  shortenID: '6j9qxUFy58',
  url: 'www.google.com',
  visitedAt: [
    {
      timestamps: 1702801419523,
      _id: new ObjectId('657eb00b0cd00ca7d2bc1f4f')
    },
    {
      timestamps: 1702801436348,
      _id: new ObjectId('657eb01c50c29d064a9b0736')
    },
    {
      timestamps: 1702801815416,
      _id: new ObjectId('657eb1979745efd6c70277fb')
    },
    {
      timestamps: 1702802115286,
      _id: new ObjectId('657eb2c3e77a78206b63dd26')
    },
    {
      timestamps: 1702802206807,
      _id: new ObjectId('657eb31ee1dd0e92e114fb37')
    },
    {
      timestamps: 1702802335263,
      _id: new ObjectId('657eb39fed9735b46e0cc78e')
    },
    {
      timestamps: 1702802591675,
      _id: new ObjectId('657eb49f0f02975f1ce96300')
    },
    {
      timestamps: 1702802694745,
      _id: new ObjectId('657eb506e3930f6479e4aa59')
    },
    {
      timestamps: 1702802791317,
      _id: new ObjectId('657eb5670ab01847226c6d9a')
    },
    {
      timestamps: 1702803258471,
      _id: new ObjectId('657eb73a52dbb7a72d182143')
    }
  ],
  createdAt: 2023-12-17T08:21:36.795Z,
  updatedAt: 2023-12-17T08:54:18.473Z,
  __v: 0
}
null
async function redirectToUrl(req, res) {

    const shortenID = req.params.id

    const urlBody = await URL.findOneAndUpdate({
        shortenID
    },
        {
            $push: {
                visitedAt: {
                    timestamps: Date.now(),
                }
            }
        })
    // console.log(urlBody);
    if (urlBody) {
        // Redirect to the URL if the document exists
        return res.redirect(urlBody.url);
    } else {
        // Handle the case where no document was found
        return res.status(404).send('URL not found');
    }
}

推荐答案

res.redirect()%的方法:

重定向到从指定路径派生的URL.

重定向可以是用于重定向到不同站点的完全限定的URL.

重定向可以相对于主机名的根.

重定向可以相对于当前URL.

最后一个是您已经实现的.

假设您的路由是:

http://example.com/shortenID

你拿着req.params.shortenID美元,试图找到那份文件.然后告诉Express重定向至此处:

return res.redirect(urlBody.url);

好的,这意味着:

http://example.com/www.google.com

它正被redirectToUrl功能拾取.正如你现在可能已经猜到的,当你取req.params.shortenID的时候,它的值现在是www.google.com.您没有包含{shortenID: 'www.google.com'}的文档,因此Mongoose找不到要更新的文档.

然后,您的代码判断if (urlBody),现在它是false,其中包含请求的最终位置,

return res.status(404).send('URL not found');

要解决这个问题,只需将一个完全限定的url保存到文档的url属性中,因此只需保存https://www.google.com而不是www.google.com.

Node.js相关问答推荐

无法从ejs Web应用程序中的正文中提取数据

MongoDB-如何验证Document字段以仅允许特定的文件扩展名?

如何呈现ejs.renderFile中包含的子模板?

如何在nodejs中打印pdf

Promise 和 Azure 语音转文本

GitLab 依赖扫描需要源代码中的 package-lock.json 才能执行

合并Shift对象数组以创建最终的排班表

Cypress.io - 如何等待返回调用属性的方法的结果?

Mongoose post中间件触发deleteMany

处理 UTC 日期和future

如何从名字,中间名,姓氏中获取全名并在结果中搜索匹配?

Nodejs mongoose 在一个查询中从多个集合中获取结果

在数组的另一个对象中获取数组的mongoose 对象

如何在拦截器中发送不同的请求?

使用新react 18.0.0 的 create-react-app my-app redux 错误

Nodejs-console.error vs util.debug

如何可靠地散列 JavaScript 对象?

如何解决 Socket.io 404(未找到)错误?

Meteor - collection.find() 总是返回所有字段

node.js 示例