[SOLVED] Using documents (as array) in Simple Schema

Hi guys !

I’m actually trying to put some arrays in a collection, which will be a document fetch of an other collection. Perhaps, actually, im getting a traceback in console.

I don’t know why, it seems to be unable to load the collection. Here’s the code :slightly_smiling_face: :

import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';

import SimpleSchema from 'simpl-schema';
SimpleSchema.extendOptions(['autoform']);

Project = new Mongo.Collection('project');

Project.allow({
    insert: function(userId, doc) {
        return !!userId;    
    },
    update: function (userId, doc) {
        return !!userId;
    },
    
});

// LOAD HERE OTHE COLLECTIONS
import { Partners } from '../partners/collection.js';
partnersLoad = Partners.find({}).fetch();

Project_Schema = new SimpleSchema ({
    name: {
        type: String,
        label: "Name *"
    },
    subtitle: {
        type: String,
        label: "Subtitle *"
    },
    desc: {
        type: String,
        label: "Description",
        optional: true
    },
    client: {
        type: Array,
        optional: true
    },
    "client.$": {
        type: String,
        label: "Client name",
        allowedValues: Partners
    },
    state: {
        type: String,
        label: "State *",
        allowedValues: ['To do', 'Doing', 'Done']
    },
    dateBegin: {
        type: Date,
        label: "Project beginning",
        optional: true
    },
    dateEnd: {
        type: Date,
        label: "Project Ending",
        optional: true
    },
    priority: {
        type: String,
        label: "Priority *",
        allowedValues: ['Low', 'Normal', 'High']
    },
    owner: {
        type: String,
        label: "Owner *"
    },
    title: {
        type: String,
        label: "Title",
        optional: true
    }

});

Meteor.methods({
    projectDelete: function(id) {
        Project.remove(id)
    },
});

Project.attachSchema(Project_Schema);

Here is the other collection :


import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';

import SimpleSchema from 'simpl-schema';
SimpleSchema.extendOptions(['autoform']);

Partners = new Mongo.Collection('Partners');

Partners.allow({
    insert: function(userId, doc) {
        return !!userId;    
    }
});

PartnersSchema = new SimpleSchema ({
    name: {
        type: String,
        label: "Partner name"
    },
    surname: {
        type: String,
        label: "Partner surname"
    }
});

Meteor.methods({
    partnersDeletePartners: function(id) {
        Partners.remove(id)
    },
});

Partners.attachSchema(PartnersSchema);

The traceback i’m actually getting :

W20190131-08:23:12.544(1)? (STDERR)       throw error;
W20190131-08:23:12.544(1)? (STDERR)       ^
W20190131-08:23:12.544(1)? (STDERR)
W20190131-08:23:12.544(1)? (STDERR) TypeError: Cannot read property 'find' of undefined
W20190131-08:23:12.544(1)? (STDERR)     at collection.js (collections/standard/project/collection.js:21:25)

Does anyone dit it already ? What am i doing bad ? Thanks a lot in advance !

Happy coding anyway !

Brawcks

Okay, i’ve found this post : Storing many to one objects in simpleschema mongo collection

According to the doc, he seems to be right, i’ve also changed my schema to this :

Project_Schema = new SimpleSchema ({
    name: {
        type: String,
        label: "Name *"
    },
    subtitle: {
        type: String,
        label: "Subtitle *"
    },
    desc: {
        type: String,
        label: "Description",
        optional: true
    },
    client: {
        type: String,
        label: "Client name",
        allowedValues() {
            return Partners.find({}).map(s => s._id);
        }
    },
    state: {
        type: String,
        label: "State *",
        allowedValues: ['To do', 'Doing', 'Done']
    },
    dateBegin: {
        type: Date,
        label: "Project beginning",
        optional: true
    },
    dateEnd: {
        type: Date,
        label: "Project Ending",
        optional: true
    },
    priority: {
        type: String,
        label: "Priority *",
        allowedValues: ['Low', 'Normal', 'High']
    },
    owner: {
        type: String,
        label: "Owner *"
    },
    title: {
        type: String,
        label: "Title",
        optional: true
    }

});

This time, i’m not getting any errors from meteor, perhaps there is no value displayed in the select field which is generated by simple schema.

My question is : Why the data isn’t displaying ? If the Partners collection wasn’t found, i would get an error in console i guess.

Is there some specific rules for collections ?

Thanks a lot to those who can answer.

Happy coding anyway,

Brawcks.

EDIT: Values are nicely displayed when i do a ‘console.log’ of the function content, on any of my page events. It should be the same in collection ?

EDIT 2 : Solution FOUND: IN fact, when we use collection and simple schema, we’re able to display via autoform a specific collection form on a template (via : collection"myCollection"). Perhaps, other collections do not exists in this template (We only aim one), so we also have to subscribe to the second collection on the template creation.

Then, records, are displayed.

Happy coding anyway !