gpt4 book ai didi

javascript - nodejs函数返回未定义

转载 作者:太空宇宙 更新时间:2023-11-04 02:28:37 25 4
gpt4 key购买 nike

背景

我制作了一个小网站来举办小费比赛。我正在使用 Chartjs 来绘制 garphs。我创建了一个函数(createCjsDonutDataWinnerPickData)将图表的数据格式化为适合图表js的格式。

问题

由于某种原因,函数 (createCjsDonutDataWinnerPickData) 返回未定义。我认为这是由于“return”在函数处理完成之前返回所致。

我在两个地方调用该函数。第一次是页面渲染前的测试,第二次是渲染页面时的测试。

从页面渲染函数中提取

Point.findOne({user:req.user.id, competition:comp.id, fixture:fixture.id}).exec(function (err, points){
if (err) {console.log('ERR: fixtures pick page on COMP lookup')}
else {
console.log('TEST FUNCTION');
console.log(createCjsDonutDataWinnerPickData(fixture._id,comp._id,createIdLookup(teams)));
res.render('fixturePick.ejs', {
user : req.user, // get the user out of session and pass to template
fixture: fixture,
pick: pick,
teams: createIdLookup(teams),
draw: draw,
round: round,
competition: comp,
points: points,
gWinnerPickGraph: JSON.stringify( createCjsDonutDataWinnerPickData(fixture._id,comp._id,createIdLookup(teams)) ),
successMsg: req.flash('successMsg'),
dangerMsg: req.flash('dangerMsg'),
warningMsg: req.flash('warningMsg')
});
}
});

函数createCjsDonutDataWinnerPickData

function createCjsDonutDataWinnerPickData(fixtureID,competitionID,teamLookup){
var Statistic = require('../app/models/statistic');
var Fixture = require('../app/models/fixture');
var async = require('async');

try {
async.waterfall([
function(cb_ReturnData){
var chartData =[];
Statistic.findOne({fixture:fixtureID, competition:competitionID,type:'winnerPickNumber'}).populate('fixture').exec(function (err,statData){
if (err) {console.log('ERROR in preparing data for graph');throw (err)}
else {

async.each(statData.data, function(dataPoint,cb_PrepData){
var sliceData = {};
if (statData.fixture.homeTeam._id == dataPoint.teamID){
//console.log('Found data for home team')
sliceData = {value: dataPoint.number, color:"rgba(151,187,205,0.5)", highlight: "rgba(151,187,205,0.75)", label:teamLookup[dataPoint.teamID].name };
}
else
{
//console.log('Found data for away team')
sliceData = {value: dataPoint.number, color:"rgba(220,220,220,0.5)", highlight: "rgba(220,220,220,0.75)", label:teamLookup[dataPoint.teamID].name };
}
//console.log('Pusihgin slice data to data')
chartData.push(sliceData);
cb_PrepData();
}, function(err){
if (err) {
console.log('ERROR in creating data for WinnerPickGraph');
cb_ReturnData(err);
}
else {
//console.log('CHART DATA IN INNER ASYNC');
//console.log(chartData);
cb_ReturnData(null, chartData);
}
});

}
});


}
],function(err,chartData){
if (err) {console.log('ERROR in preparing return data')}
else {
console.log('HERE IS THE FINAL RETURN');
console.log(chartData);
return (chartData);
}
});
}
catch (err){
//probably end up here because there is no points History Data
console.log('threw erro returning undefined');
return undefined;
}
}

控制台输出

GET /fixturePick?competition=542a5ffa736e3e35532f2d24&fixture=542c9ae12367c9209a739150 302 5.532 ms - 58
PATH: /fixturePick?competition=542a5ffa736e3e35532f2d24&fixture=542c9ae12367c9209a739150
GET / 200 11.291 ms - 1515
GET /images/grassBackground.jpg 200 5.533 ms - 145369
POST /login 302 459.431 ms - 228
TEST FUNCTION
undefined
GET /fixturePick?competition=542a5ffa736e3e35532f2d24&fixture=542c9ae12367c9209a739150 200 1609.303 ms - 4984
GET /images/team/logo/sm/53fc6399b918a6b661d423b4.png 200 2.720 ms - 15747
HERE IS THE FINAL RETURN
[ { value: 1,
color: 'rgba(220,220,220,0.5)',
highlight: 'rgba(220,220,220,0.75)',
label: 'Melbourne Victory' },
{ value: 2,
color: 'rgba(220,220,220,0.5)',
highlight: 'rgba(220,220,220,0.75)',
label: 'Western Sydney Wanderers' } ]
HERE IS THE FINAL RETURN
[ { value: 1,
color: 'rgba(220,220,220,0.5)',
highlight: 'rgba(220,220,220,0.75)',
label: 'Melbourne Victory' },
{ value: 2,
color: 'rgba(220,220,220,0.5)',
highlight: 'rgba(220,220,220,0.75)',
label: 'Western Sydney Wanderers' } ]
GET /images/team/logo/sm/53fc6399b918a6b661d423b5.png 200 2.368 ms - 13144
GET /images/none.png 200 1.857 ms - 195

我可能用 async.foreach 和 async.waterfall 使这个过程变得过于复杂,但我对为什么该函数不断返回未定义的想法已经没有了。

最佳答案

createCjsDonutDataWinnerPickData 将始终返回 undefined,因为它是异步的。要获取结果句柄,您应该向其传递回调或返回 promise 。

以下是一些方法示例:

// callback
createCjsDonutDataWinnerPickData(fixture._id,comp._id,createIdLookup(teams), function(err, data) {
// do something with err and data now
});

// promise (es6-promise)
createCjsDonutDataWinnerPickData(fixture._id,comp._id,createIdLookup(teams)).then(function(data) {
// got some data, do something
}).catch(function(err) {
// deal with the error
});

后一种方法可以通过 es6-promise 实现。 。 bluebird效果也很好。

关于javascript - nodejs函数返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28692513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com