Javascript Meteor AutoForm:如何使用子文档数组更新模式值?
我是流星自动成型的新手。我想用国家文档更新玩家文档。下面是我的代码。如果我在AutoForm中添加Javascript Meteor AutoForm:如何使用子文档数组更新模式值?,javascript,meteor,meteor-autoform,simple-schema,Javascript,Meteor,Meteor Autoform,Simple Schema,我是流星自动成型的新手。我想用国家文档更新玩家文档。下面是我的代码。如果我在AutoForm中添加{{>afQuickField name=“country”},它将不起作用{{>afQuickField name=“name”}单独工作就可以了。如何在玩家国家/地区字段中添加整个国家/地区文档(\u id,slug,name) JS: CountrySchema = new SimpleSchema({ _id: { type: String, inde
{{>afQuickField name=“country”}
,它将不起作用<代码>{{>afQuickField name=“name”}单独工作就可以了。如何在玩家国家/地区字段中添加整个国家/地区文档(\u id,slug,name)
JS:
CountrySchema = new SimpleSchema({
_id: {
type: String,
index: true
},
slug: {
type: String,
max: 100,
index: true
},
name: {
type: String,
max: 100
}
});
Player.attachSchema(new SimpleSchema({
name: {
type: String,
label: "Player name",
index: true
},
country: {
type: [CountrySchema],
label: "country",
max: 5,
index: true,
autoform: {
options: function() {
return Country.find().fetch().map(function(object) {
return {
label: object.name,
value: object._id
};
});
}
}
}
}));
{{#autoForm id="editplayer" }}
{{> afQuickField name="name"}}
{{> afQuickField name="country"}}
{{/autoForm}}
HTML:
CountrySchema = new SimpleSchema({
_id: {
type: String,
index: true
},
slug: {
type: String,
max: 100,
index: true
},
name: {
type: String,
max: 100
}
});
Player.attachSchema(new SimpleSchema({
name: {
type: String,
label: "Player name",
index: true
},
country: {
type: [CountrySchema],
label: "country",
max: 5,
index: true,
autoform: {
options: function() {
return Country.find().fetch().map(function(object) {
return {
label: object.name,
value: object._id
};
});
}
}
}
}));
{{#autoForm id="editplayer" }}
{{> afQuickField name="name"}}
{{> afQuickField name="country"}}
{{/autoForm}}
我添加了
SimpleSchema.debug=true代码>控制台日志显示“editplayer”上下文的SimpleSchema无效键
如果要将国家/地区
呈现为数组字段,可以使用组件:
<template name="editPlayer">
{{#autoForm id="editplayer" collection="Player" type="update" doc=currentPlayer}}
{{> afQuickField name="name"}}
{{> afArrayField name="country"}}
<button type="submit">Submit</button>
{{/autoForm}}
</template>
请注意,我假设您要更新player
文档。因此,我设置了属性doc=currentPlayer
,并通过currentPlayer
helper函数指定了一个player
文档。如果设置数据上下文,例如通过路由中的,则可以使用doc=this
如果您想要一个简单的选择表单,您当前的数据结构可能需要一个变通方法。问题是[CountrySchema]
没有扩展到正确的模式键。因此,您需要显式指定完整模式,并使用Meteor helper函数(currentPlayer
)转换所选选项:
非常感谢@Matthias Eckhart。我需要在选择框中显示国家,但它位于文本框中。你能让我知道它是选择框吗。你好,Matthias Eckhart。我尝试了更新的答案。但我得到了验证错误。你能帮我吗@风铃草:你的钩子设置不正确。您需要将您的\u id
值转换为对象。是@Matthias Eckhart通过AutoForm.addHooks
解决。再次非常感谢。你是我的救世主
if (Meteor.isClient) {
AutoForm.debug();
Template.editPlayer.helpers({
currentPlayer: function () {
let player = Player.findOne();
if (player) player.country = _.map(player.country, (country) => country._id);
return player;
}
});
var playerUpdateHook = {
before: {
update: function (doc) {
doc['$set'].country = _.map(doc['$set'].country, (countryId) => Country.findOne({_id: countryId}));
return doc;
}
}
};
AutoForm.addHooks('editplayer', playerUpdateHook);
}
if (Meteor.isServer) {
Meteor.startup(function () {
let country = {slug: 'austria', name: 'Austria'};
let countryId = Country.insert(country);
country = Country.findOne({_id: countryId});
Player.insert({name: 'Matthias Eckhart', country: [country]});
});
}
Player = new Mongo.Collection("player");
Country = new Mongo.Collection("country");
CountrySchema = new SimpleSchema({
_id: {
type: String,
index: true
},
slug: {
type: String,
max: 100,
index: true
},
name: {
type: String,
max: 100
}
});
Player.attachSchema(new SimpleSchema({
name: {
type: String,
label: "Player name",
index: true
},
country: {
type: [CountrySchema],
label: "country",
max: 5,
index: true
},
'country.$': {
type: String,
autoform: {
options: function () {
return Country.find().map((object) => ({label: object.name, value: object._id}));
}
}
}
}));