问题是,尽管我认为我已经处理了所有情况,但我遇到了UNhandledPromiseRejection错误。代码从profileRoutes到Controller再到Utils,首先出现错误。
在profileRoutes.js内
R
在controllers / index.js内部
library(lattice)
data <- iris
data <- as.data.frame(data)
data <- data[-c(1,2,3,4)]
data$Class <- as.factor(c(rbinom(50,1,0.7),rbinom(100,1,0.1)))
data$species2 <- as.factor(rbinom(150,1,0.85))
data2 <- as.data.frame(table(data$Species,data$Class))
barchart(Var1~Freq,groups=Var2,data=data2,stack=TRUE,
auto.key=list(title = "Class",columns=2))
data3 <- as.data.frame(table(data$species2,data$Class))
barchart(Var1~Freq,groups=Var2,data=data3,stack=TRUE,
auto.key=list(title = "Class",columns=2))
在utils / index.js内部
router.get('/:username', async (r, s) => {
try{
let profileData = await getProfileData(r.params.username);
s.json({ success: true, payload: profileData });
}catch(err){
console.log('ending request processing by responding a error');
s.status(500).json({ success: false, message: 'err[0].message' });
}
});
向
const fetchQueue = [getUserRepos];
async function getProfileData(username) {
let profileData = {};
try{
let results = await Promise.all(fetchQueue.map(item => item(username)));
for (let i = 0; i < results.length; i++) {
profileData[getKeys[i]] = results[i];
}
return profileData;
}catch(err){
console.log('error log in controller/index getProfileData function');
throw err;
}
}
const getUserRepos = async (username) => {
try {
// const res = await utils.gqlSender(username, 'userRepos', { createdAt });
const res = await utils.gqlSender(username, 'userReposData');
return res.user.repositories;
} catch (err) {
console.log('error log in controller/index getUserRepos function');
throw err;
}
};
发出获取请求时的堆栈跟踪为-
const gqlSender = async (username, type, opt = {}) => {
axios.post('', {
query: gqlQuery(username, type, opt) // generates a needed graphQL query
}).then(res => {
if(res.data.errors) { // this is where the error is recieved and so i reject promise.
console.log('bef@@@re');
return Promise.reject (res.data.errors);
}
console.log('###',res.data);
return res.data;
}).catch(err => {
console.log('error in making axios request inside utils/index gqlSender function');
throw err;
// return Promise.reject(err);
});
我不认为我没有错过任何诺言。 任何帮助表示赞赏。谢谢。
我以前已经提到了这些答案-
答案 0 :(得分:0)
您的gqlSender
函数未return
履行将被拒绝的诺言,因此不会在任何地方进行处理。您应该写
const gqlSender = (username, type, opt = {}) => {
return axios.post('', {
// ^^^^^^
query: gqlQuery(username, type, opt) // generates a needed graphQL query
}).then(res => {
if (res.data.errors) {
console.log('error in making axios request inside utils/index gqlSender function');
throw res.data.errors;
} else {
console.log('###',res.data);
return res.data;
}
});
};
或
const gqlSender = async (username, type, opt = {}) => {
// ^^^^^
const res = await axios.post('', {
query: gqlQuery(username, type, opt) // generates a needed graphQL query
});
if (res.data.errors) {
console.log('error in making axios request inside utils/index gqlSender function');
throw res.data.errors;
} else {
console.log('###',res.data);
return res.data;
}
}