Reputation: 446
Accounting = new Mongo.Collection('accounting');
var AccountingSchema = new SimpleSchema({
"caseID":{
label:"Case",
type: String,
autoform: {
type: "universe-select",
afFieldInput: {uniPlaceholder:"Select..."},
options: function(){
return Cases.find().map(function (c) {
return {label: c.caseName, value: c._id};
});
}
}
},
caseName:{
type: String,
optional: true
},
"category":{
type: String,
autoform: {
afFieldInput: {
type: "universe-select",
uniPlaceholder:"Select..."
},
options:
[
{label: "Personal", value: "Personal"},
{label: "Therapy", value: "Therapy"},
{label: "Other", value: "Other"}
]
}
},
"cost":{
type: Number
},
"description":{
type: String,
max: 100,
optional: true,
autoform: {
afFieldInput: {
type: "textarea",
rows: 5
}
}
},
"accountingFiles": {
type: [String],
optional: true,
autoform: {
type: 'hidden'
},
autoValue: function(){
if (this.isInsert) {
return this.value;
} else if (this.isUpsert) {
return {$setOnInsert: this.value};
}
}
},
"expenses":{
type: [Object],
optional: true,
autoValue: function() {
var caseID = this.field('caseID');
var category = this.field('category');
var cost = this.field('cost');
var description = this.field('description');
var accountingFiles = this.field('accountingFiles');
var seq = 1;
accountingFiles.value = (!!accountingFiles.value ? accountingFiles.value:[]);
if(this.isInsert){
return [{
"_id":Random.id(),
date: new Date(),
category: category.value,
cost: cost.value,
seq:seq,
description : description.value,
accountingFiles: accountingFiles.value,
createdBy: this.userId
}];
} else {
return {
$push: {
"_id":Random.id(),
date: new Date(),
category: category.value,
cost: cost.value,
seq:seq,
description : description.value,
accountingFiles: accountingFiles.value,
createdBy: this.userId
}
};
}
}
},
"expenses.$._id":{
type: String
},
"expenses.$.seq":{
type: Number,
optional: true
},
'expenses.$.date': {
type: Date,
optional: true
},
"expenses.$.category":{
type: String
},
"expenses.$.cost":{
type: Number,
optional: true,
},
'expenses.$.description': {
type: String,
optional: true
},
"expenses.$.accountingFiles":{
type: [String],
optional: true
},
'expenses.$.createdBy':{
type: String,
optional: true
},
"grandTotal":{
type: Number,
optional: true
},
"createdBy":{
type: String,
label: "Created By",
optional: true,
autoValue: function() {
if (this.isInsert) {
return this.userId;
} else if (this.isUpsert) {
return {$setOnInsert:this.userId};
} else {
this.unset(); // Prevent user from supplying their own value
}
}
},
"updatedBy":{
type: String,
optional: true,
label: "Updted By",
autoValue: function() {
if (this.isUpdate) {
return this.userId;
}
},
denyInsert: true,
},
"createdAt": {
optional: true,
type: Date,
autoValue: function() {
if (this.isInsert) {
return new Date;
} else if (this.isUpsert) {
return {$setOnInsert: new Date};
} else {
this.unset(); // Prevent user from supplying their own value
}
}
},
"updatedAt": {
type: Date,
optional: true,
autoValue: function() {
if (this.isUpdate) {
return new Date();
}
},
denyInsert: true,
optional: true
}
});
Accounting.attachSchema( AccountingSchema );
//server method
addAccounting: function(doc){
check(doc, Accounting.simpleSchema());
//add auto incrmenting counters for each expense
Meteor.call('checkFirstIndex','expenses');
doc.expenses[0].seq = Meteor.call('getNextSequence','expenses');
return Accounting.insert(doc);
}
I am using a method to insert a document :
methodName: function(doc){
doc.someObject.$.element = someValue; //this gives an error
table.insert(doc);
}
I am trying to set the "element" of the "someObject" object before the file is inserted but getting an error. How can this be done?
NOTE: I made an error in original question. type for someObject should be an array of objects. I have corrected it.
NOTE: I have uploaded the full code set. The idea is that I keep all the entries for a caseID instead of having two tables. When ever a document is uploaded is added under the expenses object. However I want to set the expenses.$.seq on the server. This is mongodb equivalent of a auto increment field. I am using autoForm.
Upvotes: 1
Views: 432
Reputation: 1640
I think you want to change your schema to this.
someObject:{
type: Object
},
"someObject.element":{
type: String
}
Using $ is a placeholder, only used for arrays ( I believe )
https://github.com/aldeed/meteor-simple-schema/blob/master/README.md#schema-keys
EDIT: Updated answer after question was updated
Ok, based on your change, I think the issue you are having is that you are trying to get to the array value by using "$". The "$" is used as a placeholder in the schema, but it isn't valid javascript outside of the schema.
Try this instead.
methodName: function(doc){
doc.someObject[0]element = someValue;
table.insert(doc);
}
If the "someValue" variable is suppose to be an array then do this instead.
methodName: function(doc){
doc.someObject = someValue;
table.insert(doc);
}
Upvotes: 2