如題,
就算用了await語法,程序還是不會按照順序執行,
app.js
router.post('/getPersonallyQuestion', async (req,res,next)=>{
const errQ = req.body;
var err_temp;
const errorQ = (errQ) => {
return new Promise(async (resolve, reject) => {
for (let i = 0; i < errQ.errQ.length; i += 1) {
const result = await db.getDB().collection(collection).find({ pname: errQ.errQ[i] }).toArray();
err_temp += JSON.stringify(result);
}
resolve(err_temp);
});
};
reQ = errQ["errQ"].length;
if(reQ != 0){
err_temp = await errorQ(errQ)
.then(err_temp => err_temp)
.catch(err => res.status(500).send({ message: err }).end());
}
reQ = 4 - reQ;
const dbCount = () => {
return new Promise(async (resolve, reject) => {
const result = await db.getDB().collection(collection).countDocuments({}, function(error, numOfDocs) {
//console.log('I have '+numOfDocs+' documents in my collection');
resolve(numOfDocs);
});
});
};
dbDocumentCount = await dbCount();
var threeGroup = Math.floor( dbDocumentCount/3 );
var hml_temp;
const high = () => {
return new Promise(async (resolve, reject) => {
const result = await db.getDB().collection(collection).aggregate([
{ $sort : { total_number : -1 } },
{ $limit : threeGroup },
{ $sample: { size: 1 } }
]).toArray();
console.log("high");
console.log(result);
resolve(result);
});
};
const medium = () => {
return new Promise(async (resolve, reject) => {
const result = await db.getDB().collection(collection).aggregate([
{ $sort : { total_number : -1 } },
{ $skip : threeGroup },
{ $limit : threeGroup },
{ $sample: { size: 1 } }
]).toArray();
console.log("medium");
console.log(result);
resolve(result);
});
};
const low = () => {
return new Promise(async (resolve, reject) => {
const result = await db.getDB().collection(collection).aggregate([
{ $sort : { total_number : -1 } },
{ $skip : threeGroup+threeGroup },
{ $limit : threeGroup },
{ $sample: { size: 1 } }
]).toArray();
console.log("low");
console.log(result);
resolve(result);
});
};
const qList = () => {
return new Promise(async (resolve, reject) => {
for(let i=0; i<reQ; i++){
console.log(reQ+"reQ");
let j = random.getRandom(6); //1~6之間
if( j >=1 && j <=3 ){
let h_temp = await high()
.then(h_temp => h_temp+hml_temp)
.catch(err => res.status(500).send({ message: err }).end());
console.log("hml_temp_high");
console.log(hml_temp);
}else if( j >=4 && j <=5 ){
let m_temp = await medium()
.then(m_temp => m_temp+hml_temp)
.catch(err => res.status(500).send({ message: err }).end());
console.log("hml_temp_medium");
console.log(hml_temp);
}else{
let l_temp = await low()
.then(l_temp => l_temp+hml_temp)
.catch(err => res.status(500).send({ message: err }).end());
console.log("hml_temp_low");
console.log(hml_temp);
}
console.log( "In for---------hml_temp------------" );
console.log( hml_temp );
}
resolve(hml_temp);
console.log( "3---------hml_temp------------" );
console.log( hml_temp );
});
};
// console.log(hml_temp);
console.log("1-----err_temp--------");
console.log(err_temp);
let problem = await JSON.stringify( qList() );
console.log("2-----problem--------");
console.log(problem);
});
出來的結果是
1-----err_temp--------
undefined[{"_id":"5d4596907cf65f4d8c47288e","pname":"snake","total_number":"668000000"}][{"_id":"5d4594ccf838a13138663474","pname":"wallet","total_number":"5090000000"}]
2reQ
2-----problem--------
{}
high
[
{
_id: 5d4596097cf65f4d8c472881,
pname: 'crayon',
total_number: '677000000'
}
]
hml_temp_high
undefined
In for---------hml_temp------------
undefined
2reQ
high
[
{
_id: 5d4594ccf838a13138663474,
pname: 'wallet',
total_number: '5090000000'
}
]
hml_temp_high
undefined
In for---------hml_temp------------
undefined
3---------hml_temp------------
undefined
懇請各位大大解答,謝謝
const errorQ = (errQ) => {
...
})
const dbCount = () => {
...
}
const high = (threeGroup) => {
...
}
const medium = (threeGroup) => {
...
}
const low = (threeGroup) => {
...
}
const qList = (reQ, threeGroup) => {
...
}
router.post('/getPersonallyQuestion', async (req, res) => {
...
await errorQ(errQ)
...
await dbCount();
...
const problem = JSON.stringify(await qList(reQ, threeGroup));
res.status(200).send({ problem }).end();
})
大概修一下
這樣 router 裡只需要組合各個 function 的結果
然後送回前端
看起來會比較簡潔
要改某個 function 也不會找不到
當然
最理想的
還是切成類似
controller + service + model
的架構