I am trying to get the value of a nested array from a method that updates an Autoform generated form.
I have a schema set up like this...
Schema.ContactDetails = new SimpleSchema({
orderedBy: {
type: String,
label: "Ordered By",
optional: true,
},
[...]
)};
Orders.attachSchema(new SimpleSchema({
[...]
orderDetails: {
type: Schema.OrderDetails,
optional: true,
blackbox: true
},
[...]
)};
I then have an Autoform set up with this...
{{#autoForm collection="Orders" id="updateOrderForm" type="method-update" meteormethod="updateOrder" doc=this}}
[...]
{{/autoForm}}
And this is the updateOrder method...
updateOrder: function (doc,doc_id) {
check(doc, Orders.simpleSchema());
console.log(doc);
//Modify doc here
Orders.update({_id: doc_id}, doc);
},
The above console.log(doc); outputs the following...
{ '$set':
{ createdBy: 'o5Wye6LLMGNXLn7HY',
createdAt: Sat Apr 09 2016 22:15:27 GMT+1000 (AEST),
'contactDetails.orderedBy': 'MvCun8p6vxndj3cr8',
updatedAt: Mon Apr 11 2016 11:47:31 GMT+1000 (AEST) },
'$unset':
{ […]
My problem is that I need to get the 'contactDetails.orderedBy' value in the updateOrder method but I can't seem to access it. I have tried the following...
var orderedBy = doc.$set.contactDetails.orderedBy;
Exception while invoking method 'updateOrder' TypeError: Cannot read property 'orderedBy' of undefined
var orderedBy = doc.$set.'contactDetails.orderedBy';
Unexpected token error
Thanks in advance
Answered here...Dynamically access object property using variable
and here...Extract value from object using javascript
var orderedBy = doc.$set['contactDetails.orderedBy'];
Related
I created two seperate schemas for payments collection and memberProfile. Now I need to create a quickform so I could load all the payments relevant to a unique memberProfile.
//The code for memberPayment collection
MemberProfiles = new Mongo.Collection('memberProfiles');
RecipeSchema = new SimpleSchema({
name: {
type: String,
label: "Name"
},
desc: {
type: String,
label: "Description"
},
payments:{
type: [PaymentSchema],
autoValue: function () {
return Payments.find({ memberId="uniqueId"});
},
defaultValue: function () {
return Payments.find({memberId="uniqueId"});
},
},
// The code for payments collection
PaymentSchema = new SimpleSchema({
name:{
type: String
},
amount:{
type: String
},
memberId:{
type: String
},
});
This code doesn't work.
Looks like you're missing the schema attribute. Any autoform needs to take in a schema attribute that explicitly tells autoform to use that schema to generate the necessary form. Check this page out for demos using autoform.
{{> quickForm collection="theMongoCollection" id="theFormID" schema="theSchemaName" type="typeOfForm" }}
I am trying to use meteor-pages. In my JavaScript code I have:
Tasks = new Mongo.Collection("tasks");
Tasks.attachSchema(new SimpleSchema({
title: {
type: String,
label: "Title",
max: 200
},
complete: {
type: Boolean,
defaultValue: false,
label: " ",
autoform: {
type: "boolean-checkbox"
}
},
dueDate: {
type: Date,
label: "Due Date",
optional: true,
autoform: {
type: "pickadate"
}
}
}));
Pages = new Meteor.Pagination(Tasks, {
templateName: "tasksPaginated"
})
In my html, I have:
<template name="TaskList">
Before
{{> tasksPaginated}}
After
</template>
<template name="tasksPaginated">
{{> pages}}
{{> pagesNav}} Bottom navigation
</template>
When I try to browse to the page, I get the following error:
Exception in delivering result of invoking 'pages_tasks/CountPages':
Error
at Connection._livedata_result (http://localhost:3000/packages/ddp-client.js?hash=bc32a166cd269e06a394f9418e0024d805bab379:4736:23)
at onMessage (http://localhost:3000/packages/ddp-client.js?hash=bc32a166cd269e06a394f9418e0024d805bab379:3385:12)
at http://localhost:3000/packages/ddp-client.js?hash=bc32a166cd269e06a394f9418e0024d805bab379:2736:11
at Array.forEach (native)
at Function..each..forEach (http://localhost:3000/packages/underscore.js?hash=cde485f60699ff9aced3305f70189e39c665183c:149:11)
at SockJS.self.socket.onmessage (http://localhost:3000/packages/ddp-client.js?hash=bc32a166cd269e06a394f9418e0024d805bab379:2735:11)
at SockJS.REventTarget.dispatchEvent (http://localhost:3000/packages/ddp-client.js?hash=bc32a166cd269e06a394f9418e0024d805bab379:175:22)
at SockJS._dispatchMessage (http://localhost:3000/packages/ddp-client.js?hash=bc32a166cd269e06a394f9418e0024d805bab379:1160:10)
at SockJS._didMessage (http://localhost:3000/packages/ddp-client.js?hash=bc32a166cd269e06a394f9418e0024d805bab379:1218:18)
at WebSocket.that.ws.onmessage (http://localhost:3000/packages/ddp-client.js?hash=bc32a166cd269e06a394f9418e0024d805bab379:1365:17)
Any ideas what I could be doing wrong?
The documentation for meteor-pages says you can initialise it like this:
this.Pages = new Meteor.Pagination("tasks");
And this will automatically pick up a template called "tasks". The other way (which I think you want) is to specify a different template, in which case your code should be :
this.Pages = new Meteor.Pagination("tasks", {itemTemplate: "tasksPaginated"});
That should solve it for you
Make sure the code
Pages = new Meteor.Pagination(Tasks, {
templateName: "tasksPaginated"
})
don't run before your Template.tasksPaginated is available globally
In my Meteor app, I have a simple array field called relatedSentences. It is defined using SimpleSchema
relatedSentences: {
type: [String],
label: "Related Sentence",
optional: false,
defaultValue: []
},
It's data can be seen in the Mongo console:
"_id" : "ei96FFfFdmhPXxRWb",
"sentence" : "the newest one",
"relatedSentences" : [
"Ls6EyBkbcotcyfLyw",
"6HKQroCjZhG9YCuBt"
],
"createdAt" : ISODate("2015-10-25T11:21:25.338Z"),
"updatedAt" : ISODate("2015-10-25T11:41:39.691Z")
But when I try to access this field using this, it is returned as a raw string.
Template.showSentence.helpers({
translations: function() {
console.log("related: " + this.relatedSentences);
Meteor.subscribe('sentences', function() {
var relatedSentences = Sentences.find({_id: {$in: this.relatedSentences} }).fetch();
console.log("rel count" + relatedSentences.length);
return relatedSentences;
});
}
});
In the console I get an error. See the return value from the this.relatedSentences. It is the contents of the array as a string, with a comma interpolated.
related: Ls6EyBkbcotcyfLyw,6HKQroCjZhG9YCuBt
selector.js:595 Uncaught Error: $in needs an array
Not sure what is going on here.
Some Progress
I have made some progress, but not yet at a solution. By adding blackbox: true to the SimpleSchema definition, what looks like an array is now returned... but alas it is still failing. See below.
relatedSentences: {
type: [String],
label: "Related Sentence",
optional: false,
blackbox: true,
defaultValue: []
},
Now I get the results below in the console. The values are now being returned as a quoted array, which is what I expected. But the $in is still not seeing it as an array.
["Ls6EyBkbcotcyfLyw", "6HKQroCjZhG9YCuBt"]
selector.js:595 Uncaught Error: $in needs an array
How did the data get populated
In answer to #Kyll - this is how the data was originally populated. I am using AutoForm,
{{> afQuickField name='relatedSentences.0' value=this._id type="hidden"}}
but then adding the array data via a hook.
AutoForm.addHooks('translateForm', {
onSuccess: function (operation, result, template) {
Meteor.subscribe('sentences', function() {
var translatedSentence = Sentences.findOne(result);
var originalSentenceId = translatedSentence.relatedSentences[0]
Sentences.update(
{ _id: originalSentenceId},
{ $push: { relatedSentences: result}
});
Router.go('showSentence',{ _id: originalSentenceId } );
});
}
});
The problem here is the scope of this. You are referring to it inside the subscribe function, where this has a different context. Set a varibale to this in the context where it works, then use that variable instead:
Template.showSentence.helpers({
translations: function() {
console.log("related: " + this.relatedSentences);
var this_related_sentences = this.relatedSentences;
Meteor.subscribe('sentences', function() {
var relatedSentences = Sentences.find({_id: {$in: this_related_sentences} }).fetch();
console.log("rel count" + relatedSentences.length);
return relatedSentences;
});
}
});
Let's say, we have this schema :
Schemas.MyCollection = new SimpleSchema({
something: {
type: Object
}
});
I want to insert something into MyCollection. For example :
var myobj = {
aaaaaa: 11111,
bbbbbb: 22222
};
MyCollection.insert({something: myobj});
We end up with this :
{
_id: "someId",
something: {}
}
When I disable simple schema checking (collection2), everything works as one expected.
Simple-schema did not report an error (collection2) so why it is invalid?
#Seraph your schema is wrong
Schemas.MyCollection = new SimpleSchema({
something: {
type: Object
},
'something.aaaaa': {
type: String
}
});
and so on you have to write every property the object has or you can do blackbox: true if you don't want to validate the object:
something: {
type: Object,
blackbox: true
}
Also if it's server-side operation you can do myCollection.insert(doc, {validate: false});
just read the docs https://atmospherejs.com/aldeed/collection2 :)
Here is the reference to help you understand more:
https://github.com/aldeed/meteor-simple-schema#blackbox
I have a schema like so (fluff cut out):
Schemas.people = new SimpleSchema({
note: {
type: [Schemas.notes],
optional: true,
defaultValue: []
},
updates: {
type: [Schemas.updates],
optional:true,
autoValue:function(){
if (this.isInsert) {
return [{
at: new Date,
user_id: this.userId
}];
}
return {
$push:{
at: new Date,
user_id: this.userId
}
}
}
}
});
And the notes schema looks like:
Schemas.notes = new SimpleSchema({
note: {
type: String,
autoform: {
afFieldInput:{
type:"textarea"
}
},
optional: true
},
updates: {
type: [Schemas.updates],
optional:true,
autoform:{
omit:true
},
autoValue:function(){
if (this.isInsert) {
return [{
at: new Date,
user_id: this.userId
}];
}
return {
$push:{
at: new Date,
user_id: this.userId
}
}
}
}
});
And the updates schema is super simple:
Schemas.updates = new SimpleSchema({
at: {
type: Date
},
user_id:{
type: Meteor.ObjectID
}
});
The "updates" field on the people schema saves the date/user id as expected when an update is made. However, it fails on the notes schema:
SimpleSchema invalid keys for "blablabla" context:
0: Object
name: "note.0.updates.0.at"
type: "keyNotInSchema"
value: Mon May 11 2015 11:57:58 GMT-0400 (Eastern Daylight Time)
1: Object
name: "note.0.updates.0.user_id"
type: "keyNotInSchema"
value: "abcd1234"
I believe that the "name" should look like "people.note.0.updates.0.at" but I'm unsure that this assumption is correct and I'm completely unsure how to go about making that happen.
Update:
Code used to update people
{{#autoForm collection="people" id=formId type="update" class="update" autocomplete="off" doc=getDocument autosave=true template="quickform"}}
{{> afQuickField name='note' template="quickform" }}
{{/autoForm}}
formId returns a randomish ID string and getDocument passes in the correct collection.
Schemas.notes._schemaKeys does not list the at and user_id fields... but Schemas.people._schemaKeys does.
People schema shows: [..., "updates.$.at", "updates.$.user_id", ...]
Notes schema shows: ["note", "updates", "updates.$"]
How bizarre.
Note that Meteor uses standard JavaScript syntax and therefore has the same restrictions, for example as you already realized order of code is important.
Let's have a look.
Schemas.notes = new SimpleSchema({
updates: {
type: [Schemas.updates]
}
}
There are no nested functions in this code, therefore every code of line will be executed, before Meteor continues with the next Schema definition. Schema.updates will be dereferenced immediately, although it isn't set yet. type will be an array containing null and that finally makes SimpleSchema assume that no fields are allowed at all.
The issue is with the order in which the schemas are declared. Which I suppose makes sense? I was declaring "notes" before "updates", and "people" last. Putting "updates" first fixed the issue completely.
I'm going to report this as a possible bug to the collection repo.