【发布时间】:2019-08-04 21:43:33
【问题描述】:
方法调用“插入所有短信数据”运行不止一次我试图阻止使用计数器变量,但在客户端它运行一次,在服务器端它运行 x 次,如图所示,从而将数据添加到 db我不想要的多个时间,这应该只添加到数据库一次。我还想在方法调用“插入所有短信数据”中添加另一个调用,它应该将 x 作为循环运行。我卡在这里。 流星1.8反应16.8
imports/api/kuser.js
'Find all numbers' (smsText) {
if(!this.userId) {
throw new Meteor.Error('not-authorized');
}
let counter = 0;
return userKisandb.find({
userId: this.userId
}).fetch().map((Mob) => {
// return Mob.Mnumber
if(Mob.Mnumber) {
Meteor.call('Send to all',Mob.Mnumber,smsText,(err,resp) => {
//sms.js
if(err){
console.log("send all numbers error2", err);
} else {
console.log("send all numbers ", resp);
if(counter === 0) {
Meteor.call('Insert All sms data',smsText,counter,(err,resp1) => {
//get inserted data id
//allSmsdb
if(err){
console.log("Insert All sms data error", err);
} else {
console.log("this should run only once ",counter);
//Another call to be added which should run x times
}
})
counter++
}
}
});
} //Mob.Mnumber
});
},
方法一是
'Send to all'(mob,text) {
return "sucess";
},
方法2是
'Insert All sms data' (smstext,counter) {
if(!this.userId) {
throw new Meteor.Error('not-authorized');
}
console.log("Inserted same data x times",counter);
if(counter === 0) {
return allSms.insert({
smstext,
userId: this.userId,
updatedAt: moment().valueOf(),
});
}
},
【问题讨论】: