Reputation: 1
I can't seem to get the data for the ingredients array and insert into mongodb. Anyone have any ideas?
Error in console: Uncaught TypeError: Cannot read property 'value' of undefined
My form event on client:
Template.newRecipe.events({
'submit form'(event) {
event.preventDefault();
// Get value from form element
const target = event.target;
const title = target.title.value;
const description = target.description.value;
const ingredients = target.ingredients.value; // line causing above error
// Insert a task into the collection
Meteor.call('recipes.insert', title, description, ingredients);
document.getElementById('insertRecipeForm').reset();
},
});
My server Method:
Meteor.methods({
'recipes.insert'(title, description, ingredients) {
Recipes.insert({
title,
description,
ingredients,
});
},
});
My schema:
Recipes = new Mongo.Collection('recipes');
Ingredient = new SimpleSchema({
name: {
type: String
},
amount: {
type: String
}
});
Recipe = new SimpleSchema({
title: {
type: String,
label: "Title"
},
description: {
type: String,
label: "Description",
autoform: {
rows: 6
}
},
ingredients: {
type: [Ingredient]
}
});
Recipes.attachSchema(Recipe);
Upvotes: 0
Views: 185
Reputation: 39
The error is telling you that target.ingredients doesn't exist for some reason. Without seeing your HTML we probably can't offer any better advice than that.
Upvotes: 0
Reputation: 20227
You're not inserting an object:
Recipes.insert({
title,
description,
ingredients,
});
Needs to be:
Recipes.insert({
title: title,
description: description,
ingredients: {$push: ingredients},
});
Upvotes: 0