【问题标题】:Error capturing response from TinyURL's shorten method从 TinyURL 的缩短方法捕获响应时出错
【发布时间】:2020-12-14 07:06:03
【问题描述】:

我需要从TinyUrl 库中捕获shorten 方法的返回值。我正在尝试将此返回值存储在shortUrl 变量中,然后将其保存在数据库中,如下所示:

import TinyUrl from 'tinyurl';

let shortUrl = '';

    TinyUrl.shorten(req.body.url, function (response, error) {
      if (error) console.log(error);
      console.log(response);
      shortUrl = response;
    });

    // Tudo certo para CRIAR o Site
    const { id, hits, url, short_url } = await Site.create({
      hits: 0,
      url: req.body.url,
      short_url: shortUrl,
    });

    return res.json({
      id,
      hits,
      url,
      short_url,
    });

查看console.log(response); 时,正确显示所需的返回值,但未设置shortUrl 变量。我该怎么做?

【问题讨论】:

    标签: javascript node.js sequelize.js short-url


    【解决方案1】:

    我是这样实现的:

    const shortUrl = await TinyUrl.shorten(req.body.url);
    
    const { id, hits, url, short_url } = await Site.create({
      hits: 0,
      url: req.body.url,
      short_url: shortUrl,
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-25
      • 2017-08-30
      • 1970-01-01
      • 2020-08-24
      • 2023-03-28
      • 2021-11-27
      • 1970-01-01
      相关资源
      最近更新 更多