我似乎无法获取成分数组的数据并插入到mongodb中。有人有什么想法吗?
控制台出错:未捕获TypeError:无法读取属性'值'未定义的
我在客户端的表单活动:
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();
},
});
我的服务器方法:
Meteor.methods({
'recipes.insert'(title, description, ingredients) {
Recipes.insert({
title,
description,
ingredients,
});
},
});
我的架构:
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);
答案 0 :(得分:0)
您没有插入对象:
Recipes.insert({
title,
description,
ingredients,
});
需要:
Recipes.insert({
title: title,
description: description,
ingredients: {$push: ingredients},
});
答案 1 :(得分:0)
错误告诉您target.ingredients由于某种原因不存在。如果没有看到您的HTML,我们可能无法提供更好的建议。