Node.js 如果其他函数成功,则要res.send的第一个节点函数
导言 我有3个节点函数,第一个检索前端发送的数据,第二个获取有效的API密钥,第三个函数使用前2个函数的数据发布到API 我想要一个关于第一个函数的case语句,我可以在每个案例中使用Node.js 如果其他函数成功,则要res.send的第一个节点函数,node.js,express,http-post,httprequest,Node.js,Express,Http Post,Httprequest,导言 我有3个节点函数,第一个检索前端发送的数据,第二个获取有效的API密钥,第三个函数使用前2个函数的数据发布到API 我想要一个关于第一个函数的case语句,我可以在每个案例中使用res.send(“”)。每种情况都表示函数失败或通过 我需要什么 是第二个和第三个功能中根据功能结果触发案例的一种方式 如果不是这样,如果第一个函数失败,如果第二个函数失败,如果第三个函数失败,那么如何从第一个函数开始呢 例如 switch(expression) { case 1: re
res.send(“”)
。每种情况都表示函数失败或通过
我需要什么
是第二个和第三个功能中根据功能结果触发案例的一种方式
如果不是这样,如果第一个函数失败,如果第二个函数失败,如果第三个函数失败,那么如何从第一个函数开始呢
例如
switch(expression) {
case 1:
res.send('function 1 pass')
break;
case 2:
res.send('function 1 fail')
break;
case 3:
res.send('function 2 pass')
break;
case 4:
res.send('function 2 fail')
break;
case 5:
res.send('function 3 pass')
break;
case 6:
res.send('function 3 fail')
break;
default:
res.send('something went wrong')
}
我的代码
var firstFunction = function () {
return new Promise(function (resolve) {
setTimeout(function () {
app.post('/back-end/controller', function (req, res) {
console.log(req.body);
var login = req.body.LoginEmail;
if (login.length !== 0) { // maybe use node email validation ?
console.log("First done");
res.send(200,'Success',{ user: login });
resolve({
data_login_email: login
});
} else {
console.log("Failed");
res.send(404,'Failed user not registered',{ user: login });
}
});
}, 2000);
});
};
var secondFunction = function () {
return new Promise(function (resolve) {
setTimeout(function () {
nodePardot.PardotAPI({
userKey: userkey,
email: emailAdmin,
password: password,
DEBUG: false
}, function (err, client) {
if (err) {
// Authentication failed
console.error("Authentication Failed", err);
} else {
// Authentication successful
var api_key = client.apiKey;
console.log("Authentication successful", api_key);
resolve({data_api: api_key});
}
});
}, 2000);
});
};
function thirdFunction(result) {
return new Promise(function () {
setTimeout(function () {
var headers = {
'User-Agent': 'Super Agent/0.0.1',
'Content-Type': 'application/x-www-form-urlencoded'
};
var api = result[1].data_api;
var login_email = result[0].data_login_email;
var options = {
url: 'https://pi.pardot.com/api/prospect/version/4/do/read',
method: 'POST',
headers: headers,
form: {
'email': login_email,
'user_key': userkey,
'api_key': api
},
json: true // Automatically stringifies the body to JSON
};
// Start the request
rp(options)
.then(function (parsedBody) {
console.info(login_email, "Is a user, login pass!");
console.error("Third done");
})
.catch(function (err) {
console.error("fail no such user");
// res.status(400).send()
});
}, 3000);
}
);
}
Promise.all([firstFunction(),secondFunction()]).then(thirdFunction);
Promises不是一个
switch
语句,而是使用then/catch
内置了一些逻辑功能。如果我理解这个问题,你可以这样做:
firstPromise = firstFunction()
.then(args => {
res.send('function 1 pass');
return args;
})
.catch(err => {
res.send('function 1 fail');
throw err;
});
secondPromise = secondFunction()
.then(args => {
res.send('function 2 pass');
return args;
})
.catch(err => {
res.send('function 2 fail');
throw err;
});
thirdPromise = Promise
.all([firstPromise, secondPromise])
.then(thirdFunction)
.then(args => {
res.send('function 3 pass');
return args;
})
.catch(err => {
if (/* `err` from thirdFunction, not firstFunction or secondFunction */)
res.send('function 3 fail');
throw err;
});
然后,您只需要找出检查错误来源的逻辑。一个选项是,如果
thirdFunction
失败,将fromThird
(或任何名称)属性附加到错误上,因此您的条件是if(err.fromThird)res.send('function 3 fail')代码>谢谢您的帮助,我会尝试让它正常工作并接受答案