我对承诺很新,我的后续代码产生了一些意想不到的结果,但我不知道为什么。
Main.js中的Taskrunner.SyncObjects函数应该等待填充的selectedCourses变量,但它不会立即触发。这会导致使用空的selectedCourses变量。
为什么在获得结果之前运行TaskRuner.SyncObjects函数?
Main.js
function StartSync(){
var a = Settings.LoadCourseList(standardOSPathUserData);
var b = a.then(function(){
console.log("b is running");
var selectedCourses = Settings.courseList;
return TaskRunner.SyncObjects(selectedCourses).then(function(){
fileSyncRunning = false;
});
}).catch(function(error){
console.log("StartSync Error message: " + error);
});
}
Settings.js
Settings.LoadCourseList = function(osPath){
var pathToCourseListSettings = osPath + courseListFileName;
return new Promise(function(resolve, reject){
try {
return connection.Login().then(function(){
return connection.GetCourseList().then(function(result){
var allCourses = [];
for(var p=0; p<=result.length-1;p++){
allCourses.push({courseID: result[p].courseID, courseName: result[p].courseName, selected: true});
}
courseListJSON = JSON.stringify(allCourses);
return courseListJSON;
}).then(function(courseListJSON){
fs.appendFileSync(pathToCourseListSettings,courseListJSON,encoding='utf8');
console.log("New Course settings file created and filled: " + pathToCourseListSettings);
return resolve();
});
}).then(function(){
return resolve();
});
} catch (e) {
console.log("FAIL courseList settingsfile creation - Error: " + e.code);
return reject(e);
}
});
};
答案 0 :(得分:1)
在Settings.js
中似乎从未为Settings.courseList分配值您可以尝试添加
Unhandled rejection TypeError: expecting an array or an iterable object but got [object Null]
之前的
app.get('/test/models', function(req, res) {
var values = {
where: { user_id: 7 }
};
MercadoLibre.findOne(values)
.spread(function(err, meli) {
console.log(err);
if (typeof meli !== null) {
console.log("undefined");
} else {
console.log(meli);
}
res.redirect('/dashboard');
});
});
或者更好的是,您可以采用承诺方式,并通过allCourses解决承诺。
从Settings.LoadCourseList(standardOSPathUserData)做出承诺;返回allCourse,改变
Settings.courseList = allCourses;
到
courseListJSON = JSON.stringify(allCourses);
并接收该承诺结果更改
return connection.Login().then(function(){
return connection.GetCourseList().then(function(result){
var allCourses = [];
for(var p=0; p<=result.length-1;p++){
allCourses.push({courseID: result[p].courseID, courseName: result[p].courseName, selected: true});
}
courseListJSON = JSON.stringify(allCourses);
return courseListJSON;
}).then(function(courseListJSON){
fs.appendFileSync(pathToCourseListSettings,courseListJSON,encoding='utf8');
console.log("New Course settings file created and filled: " + pathToCourseListSettings);
return resolve();
});
}).then(function(){
return resolve();
});
到
return connection.Login().then(function(){
return connection.GetCourseList().then(function(result){
var allCourses = [];
for(var p=0; p<=result.length-1;p++){
allCourses.push({courseID: result[p].courseID, courseName: result[p].courseName, selected: true});
}
var courseListJSON = JSON.stringify(allCourses);
fs.appendFileSync(pathToCourseListSettings,courseListJSON,encoding='utf8');
console.log("New Course settings file created and filled: " + pathToCourseListSettings);
return allCourses;
});
});