以下代码:我有一个模板,用户可以在表单中输入日期并按下提交按钮。现在我想检查这个输入日期是否已存在于集合中。如果存在,将弹出警报。同时我想检查用户是否正在更新现有日期或创建新日期。
Client.js
var currData = {_id: speakerDateArray[i]._id,
prID: prodIDVar,
title: currTitle,
start: speakerDateStartVar,
end: speakerDateEndVar};
if(allSpeakerDates.length == 0){
Meteor.call('insertSpeakerDate', currData);
}
else {
Meteor.call('delayedDateCompare', currData, function(err, result){
var isDateExists = result;
console.log(err + ' ' +result);
if(isDateExists === true) {
var isIdExists = Events.findOne({_id: speakerDateArray[i]._id});
if(isIdExists !== undefined){
console.log('ID vorhanden --> update');
Meteor.call('updateSpeakerDate', currData);
}
else {
alert('date already exist');
}
}
else {
var isIdExists = Events.findOne({_id: speakerDateArray[i]._id});
if(isIdExists !== undefined){
console.log('ID exists --> update');
Meteor.call('updateSpeakerDate', currData);
}
else {
console.log('ID not exist --> insert');
Meteor.call('insertSpeakerDate', currData);
}
}
});
Server.js
function compareDates(delay, data, callback) {
var tmpStart = moment(data.start).toDate();
var tmpEnd = moment(data.end).toDate();
var allSpeakerDates = Events.find({categorie: 'speaker'}).fetch();
var result;
for(i=0; i<allSpeakerDates.length; i++) {
var exStart = moment(allSpeakerDates[i].start).toDate();
var exEnd = moment(allSpeakerDates[i].end).toDate();
if (tmpStart > exStart && tmpEnd < exEnd) {
console.log('status 1');
result = true;
}
else if(tmpStart < exStart && tmpEnd > exStart) {
console.log('status 2');
result = true;
}
else if(tmpStart < exEnd && tmpEnd > exEnd) {
console.log('status 3');
result = true;
}
else {
console.log('status 4');
result = false;
}
}
setTimeout(function() {
callback(null, result);
}, delay);
}
var wrappedCompareDates = Async.wrap(compareDates);
方法
Meteor.methods({
'insertSpeakerDate': function(data){
Events.insert({
_id: data._id,
prID: data.prID,
title: data.title,
start: data.start,
end: data.end,
color: '#009C95',
categorie: 'speaker'
});
},
'updateSpeakerDate': function(data) {
console.log('Speakerdate updated: '+data._id);
Events.update({_id: data._id}, {
$set: {
title: data.title,
start: data.start,
end: data.end
}
});
},
'delayedDateCompare': function(data) {
var response = wrappedCompareDates(1000, data);
return response;
},
我收到错误“提供调用'delayedDateCompare'的结果时出现异常”。我认为这是由于流星的异步功能,但我该如何解决呢?