Hello,
I’m trying to have a collection of salaries with userId, month and year being the primary keys.
I want it to work with the following set of rules:
- If collection entry with userId - 1, month - 7, year - 2017, amount - 20 exists and I try to add the same entry again, it finds previously mentioned entry and just sums up the amount.
- If collection entry with mentioned primary keys does not exists then it creates it.
This is what I currently have
addPayment: function(userId, userFirst, userLast, month, year, amount){
if(!Meteor.userId()){
throw new Meteor.Error('No access: You are not logged in.');
}
if (!Roles.userIsInRole(Meteor.userId(), 'admin')) {
userId = Meteor.userId();
}
var entryAdded = Salaries.findOne({"userId" : {$eq: userId}, "month": {$eq: month}, "year": {$eq: year}}, {fields: {amount:1}, limit: 1}).amount;
if(entryAdded != null && entryAdded > 0 && !entryAdded){
Salaries.update(userId, {$set: {amount: amount}});
} else {
Salaries.insert({
userId:userId,
createdAt: new Date(),
userFirst: userFirst,
userLast: userLast,
month: month,
year: year,
amount:amount
});
}
},
Currently getting ‘amount’ property is undefenied.