目的传递数据
我有三个功能,我第一次从后得到的数据,第二个得到我的API密钥和第三后数据的API。我希望按照从1到3的顺序运行每个函数,我已经找到了一个在节点内使用诺言的简洁解决方案。
函数现在以正确的顺序运行,但我需要将数据从一个函数传递到下一个函数。
我需要传递:
var emailUser = req.body.email;
从我的第一个功能,我的第三个
和:
var api_key = client.apiKey;
从第二功能,我的第三个
这是我愿意,我想我几乎在那里。
var express = require('express');
var request = require('request');
// var promise = require('promise');
var nodePardot = require('node-pardot');
var bodyParser = require('body-parser');
var app = express();
var port = process.env.PORT || 8080;
// Varibles to use in second and third function
var password = 'password';
var userkey = 'hghgd7289j';
var emailAdmin = '[email protected]';
// var emailUser;
// var api_key;
// start the server
app.listen(port);
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({extended: true})); // support encoded bodies
console.log('Server started! At http://localhost:' + port);
var firstMethod = function() {
var promise = new Promise(function(resolve, reject){
setTimeout(function() {
app.post('/api/data', function (req, res) {
console.log(req.body);
// var Fname = req.body.fname;
// var Lname = req.body.lname;
var emailUser = req.body.email;
// res.send(Fname + ' ' + Lname + ' ' + emailUser);
res.send(emailUser);
});
console.log('first method completed');
resolve({data: emailUser});
}, 2000);
});
return promise;
};
var secondMethod = function(someStuff) {
var promise = new Promise(function(resolve, reject){
setTimeout(function() {
nodePardot.PardotAPI({
userKey: userkey,
email: emailAdmin,
password: password,
// turn off when live
DEBUG: true
}, function (err, client) {
if (err) {
// Authentication failed
// handle error
console.error("Authentication Failed", err)
} else {
// Authentication successful
// gets api key
var api_key = client.apiKey;
console.log("Authentication successful !", api_key);
}
});
console.log('second method completed');
resolve({newData: api_key});
}, 2000);
});
return promise;
};
var thirdMethod = function(someStuff) {
var promise = new Promise(function(resolve, reject){
setTimeout(function() {
var headers = {
'User-Agent': 'Super Agent/0.0.1',
'Content-Type': 'application/x-www-form-urlencoded'
};
var emailUser = resolve.emailUser;
var api_key = resolve.api_key;
// Configure the request
var options = {
url: 'https://pi.pardot.com/api/prospect/version/4/do/create/email',
method: 'POST',
headers: headers,
form: {
'email': emailUser,
'user_key': userkey,
'api_key': api_key
}
};
// Start the request
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
// Print out the response body
console.log("API Key",api_key);
console.log("user",emailUser);
console.log("error",body);
}
else {
console.log("Sent Data",body);
}
});
console.log('third method completed');
resolve({result: someStuff.newData});
}, 3000);
});
return promise;
};
firstMethod()
.then(secondMethod)
.then(thirdMethod);
嗯抱歉,在新的节点和承诺。我会在我的代码结尾处应用这个吗? – Beep
而不是'firstMethod()。then(secondMethod).then(thirdMethod);',尽管你可能想要修改thirdMethod的开始以显示的方式读取结果。如通过其他答案所示,通过承诺链传递的单个结果对象上的积累值是另一种方式。 –