I’m slowly learning the things I need to understand about Meteor 3, but in my template application I’m getting something I didn’t expect. Admittedly, I’m very new to the async/await stuff, so I’m guessing I’m doing something wrong here.
In the code below, I’m just setting a couple of flags for allowing registration for any user, or also system admin level users.
in the Meteor.method call I want to see if a document exists in the db before adding a new one, and if it does, then update the existing one instead. To do this in the past, I would just run a findOne
and if found, then it would update
instead of insert
. I found that with the newer findOneAsync
it was failing the test every time, and inserting new records even when one existed, so I figured it wasn’t waiting before moving on. Anyway, i have attempted to setup an async/await for that part, so it can check the status. It seems to work, but when I first insert because no data exists, it is adding a record twice.
'add.noSysAdminReg' (admReg, genReg) {
check(admReg, Boolean);
check(genReg, Boolean);
if (!this.userId) {
throw new Meteor.Error('Not able to change registration setting. Make sure you are logged in with valid system administrator credentials.');
}
console.log("Got here...");
async function currConfig() {
const curr = await SysConfig.findOneAsync({});
if (typeof curr != 'undefined') {
let configId = curr._id;
Meteor.call('edit.noSysAdminReg', configId, admReg, genReg, function(err, result) {
if (err) {
console.log(" ERROR updating sys admin reg: " + err);
} else {
console.log("Success updating sys admin reg.");
}
});
} else {
console.log("Adding new.");
return SysConfig.insertAsync({
SysAdminReg: admReg,
dateAdded: new Date(),
allowReg: genReg,
});
}
}
currConfig();
What shows up in Mongo afterward:
meteor [direct: primary] meteor> db.sysConfig.find({});
[
{
_id: 'fGcJoB6CqQseufKXq',
SysAdminReg: true,
dateAdded: ISODate('2025-04-10T19:36:45.136Z'),
allowReg: false
},
{
_id: 'ucTXykh6YAYiGFwQq',
SysAdminReg: true,
dateAdded: ISODate('2025-04-10T19:36:45.139Z'),
allowReg: false
}
]