Bind allowedValues to values from a collection in simple-schema

耗尽温柔 提交于 2019-12-24 15:03:04

问题


I'm using aldeed:simple-schema and here's the code:

Cities = new Mongo.Collection('cities');

Cities.insert({
    name: 'Oslo'
});

Cities.insert({
    name: 'Helsinki'
});

Contact = new SimpleSchema({
    city: {
        type: String,
        allowedValues: Cities.find().map((e) => e.name) // written ES6-style for readability; in fact, here goes an ES5 anonymous function definition
    }
});

What it does is explicitly binds currently existing cities from Cities collection to Contact schema's certain field's allowed values, so it's then impossible to store any other value than "Oslo" or "Helsinki".

But when posting a quickForm, the field (select, actually) has no options.

If I rewrite the mapping function to

(e) => {
    console.log(e);
    return e.name;
}

then I get

I20150911-18:07:23.334(4)? { _id: 'GLAbPa6N4W4c9GZZh', name: 'Oslo' }
I20150911-18:07:23.333(4)? { _id: 'vb64X5mKpMbDNzCkw', name: 'Helsinki' }

in server logs, which makes me think the mapping function is correct.

At the very same time, doing all this in Mongo console returns desirable result:

production-d:PRIMARY> db.cities.find().map(function (e) { return e.name; });
[ "Oslo", "Helsinki" ]

What do I do wrong? Is it impossible to fill the simple-schema's allowedValues array at the run time?

来源:https://stackoverflow.com/questions/32525570/bind-allowedvalues-to-values-from-a-collection-in-simple-schema

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!