在Node.js中使用导出的函数
概观
我正在将authorize.net付款合并到我的应用程序中。 使用他们的sdk
他们导出一个我不明白的方式的function。 我正在寻找一些清晰的,所以我可以用最好的方式。
我正在使用Angular 2
和node.js app
。
问题
从Angular2
到我在做一个http post
。 这篇文章将表单数据传递给node server
。 一旦我在节点服务器,我将数据放入数据库。 但是在这个函数里我想把数据传递给来自SDK
的authorize.net函数。
例
我发布的函数的最小版本。
function1:
updateProfile: function(req, res) { var id = req.body.id; var email = req.body.email; var first_name = req.body.first_name; var middle_name = req.body.middle_name; }
在一个名为merchant
的不同目录中,我有两个文件。 一个文件具有在商家数据库中创build用户简档的function。 他们将持有cc号码,所以我不必在我的服务器上。
function2:
function createCustomerProfile(callback) { // Stuff happens in here with the data from the first function }
我需要将数据从第一个function
传递到第二个function
。 在同一个目录中,他们有一个index.js
文件,可以像这样导出函数,
module.exports = { createCustomerProfile: require('./create-customer-profile.js').createCustomerProfile }
题
这些function就像它们一样。 我如何从函数1调用函数2并将数据传递给它? 他们把parameters
callback
。 所以我想知道这是否意味着它必须首先调用函数1作为callback
而不是我正在做的方式。
所以基本上我应该使http post
的function2。然后require
在其文件中的database insert function
的文件? 然后应用function
1作为callback
?
function1: –
var secondFn = require('path to index.js').createCustomerProfile updateProfile: function(req, res) { var id = req.body.id; var email = req.body.email; var first_name = req.body.first_name; var middle_name = req.body.middle_name; secondFn(req.body,function callback(response){ console.log('customer profile created' , response); }) }
function2: –
function createCustomerProfile(data,callback) { // perform operation var response = "custom data" callback(response) //call upon completion }
您也可以在callback函数中传递参数来检查是否有错误
- Nodejs – 将基于Promise的架构与事件混合是错误的
- 循环浏览文件夹中的文件Node.JS
- Sails.js如何在async.series()中使用for循环?
- 使用Javascript node.js如何并行处理For循环?
- Grunt Combine Media Queries – 不能调用未定义的方法“join”
- JavaScript数组迭代方向
- js / nodejs基本的初学者服务器,用于使用request.on()和querystring.parse()方法的POST方法
- Node Express和csurf – 403(Forbidden)无效的csrf标记
- 寻找节点“util.inherits”的JavaScript实现