【问题标题】:Node.js callback called twiceNode.js 回调调用了两次
【发布时间】:2013-05-21 18:40:24
【问题描述】:

我正在使用 node.js 的 sendgrid 库。目前有一个回调,它被触发了两次。

    @invite = (email, agencyId, callback) ->
  (app.get 'agencies').findOne {_id: agencyId}, {name:1}, (error, agency) ->
    sendgrid.send {
      to: email,
      from: 'yamil.asusta@upr.edu',
      subject: "You have been invited to #{agency.name}",
      html: "<html><body>Have fun</a></body></html>"
    }, (success, message) ->
      console.log arguments
      if success
        console.log callback
        callback()
      else
        console.log message

这是我从控制台得到的

{ '0': true, '1': undefined }
[Function]
{ '0': false, '1': 'Invalid JSON response from server' }
Invalid JSON response from server

问题是,我从 1 条路线调用该函数,它工作得非常好。如果我从另一个单独的路由调用它,它会触发回调,但它说回调变量(函数)未定义。

有什么线索吗?

【问题讨论】:

    标签: node.js callback coffeescript sendgrid


    【解决方案1】:

    我建议您将参数记录到@invite 函数。我怀疑这会被调用两次,一次使用正确的参数,一次使用不正确的参数,这是由于程序中其他地方的错误。

    【讨论】:

    • 我有,好几次......它只被调用一次。
    猜你喜欢
    • 1970-01-01
    • 2022-10-18
    • 2023-04-02
    • 2016-04-03
    • 1970-01-01
    • 2012-04-25
    • 2022-08-20
    • 1970-01-01
    • 2017-06-25
    相关资源
    最近更新 更多