异步调用函数的NodeJs错误

时间:2018-08-22 00:33:13

标签: node.js email callback null send

对不起,我的英文,我希望有人可以帮助我,我试图使用gmail作为smtp发送模板电子邮件,在同一文件server.js文件中一切正常,但是我试图在不同模块中分离并获得调用同一函数时出错,在调试模式下即时报错

Order By

在控制台中获取

ALTER PROCEDURE [dbo].[spPagination] -- ORDER BY id
  @PageNumber INT,
  @PageSize   INT
AS
BEGIN
  SET NOCOUNT ON;

    SELECT      Emp.id ,Emp.[firstName], Emp.[lastName], Emp.[salary], Emp.[startDateWork], Emp.age  --, Rol.[name] as Role
    FROM        [dbo].tblEmployees5m    Emp
    inner join    [dbo].[tblRoles]        Rol
    ON            Emp.roleId = Rol.id
    ORDER BY    Emp.id
    OFFSET      @PageSize * (@PageNumber - 1) ROWS
    FETCH NEXT  @PageSize ROWS ONLY OPTION (RECOMPILE);
END

我的代码

server.js

TypeError: Cannot read property 'then' of undefined
    at Object.exports.sendmail (/src/react/impex/jumbo-react-flat/mailing/server.js:44:4)
    at exports.sendregistermail (/src/react/impex/jumbo-react-flat/mailing/controllers/mailing.js:25:10)
    at Layer.handle [as handle_request] (/src/react/impex/jumbo-react-flat/mailing/node_modules/express/lib/router/layer.js:95:5)
    at next (/src/react/impex/jumbo-react-flat/mailing/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/src/react/impex/jumbo-react-flat/mailing/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/src/react/impex/jumbo-react-flat/mailing/node_modules/express/lib/router/layer.js:95:5)
    at /src/react/impex/jumbo-react-flat/mailing/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/src/react/impex/jumbo-react-flat/mailing/node_modules/express/lib/router/index.js:335:12)
    at next (/src/react/impex/jumbo-react-flat/mailing/node_modules/express/lib/router/index.js:275:10)
    at jsonParser (/src/react/impex/jumbo-react-flat/mailing/node_modules/body-parser/lib/types/json.js:109:7)

mailing.js

/src/react/impex/jumbo-react-flat/mailing/node_modules/express-mailer/lib/express-mailer.js:81
          callback(null, res.message);
          ^

TypeError: callback is not a function
    at MailComposer.returnCallback (/src/react/impex/jumbo-react-flat/mailing/node_modules/express-mailer/lib/express-mailer.js:81:11)

非常感谢您的帮助

1 个答案:

答案 0 :(得分:2)

mailer.send()返回的回调不是承诺,因此您不能在此处使用.then()。您应该按如下方式更改功能:

exports.sendmail = function (template, mailOptions) {
    app.mailer.send(template, mailOptions, function (err) {
        if (err)
            return err;
        return ;
    });
}

关于重组代码的目标,更重要的是: 由于您正在创建用于发送邮件的模块,因此所有与邮件相关的内容都应该放在其中。 mailer.extend()sendmail()应该移到那里。确保将app的实例传递给mailing.js,以便您可以初始化快递邮件。