Bind allowedValues to values from a collection in

2019-08-08 04:37发布

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?

0条回答
登录 后发表回答