Converting collection into array, and waiting for it to be ready?


#1

I have a Meteor collection called Sports and each document has a name field:

{
    "name": "badminton"
}

I am trying to convert this collection in an array, so that I can use it with the allowedValues option in meteor-autoform. I have the following function in my application’s javascript:

list_of_sports = function() {
    var results = [];
    Sports.find().map( function(sport) {
        results.push(sport.name);
    })
    console.log(results)
    return results
}

The only issue is - sometimes the above seems to return an empty array - I assume this is something to do with the data not being ready yet?

Is there perhaps a better of making the list of sports available, and also have it only return when the data is ready? (Sorry if I’m mangling the Meteor terminology there).


#2
var sportsArray = Sports.find().map(function (doc) {
  return doc.name;
});

#3

var sportsArray = Sports.find().fetch(); // fetch() returns a collection as an array

// convert sportArray into an array format autoForm can understand for selection options

var sportsOptions = sportsArray.map( function (obj) {
    return {'label': obj.name, 'value': obj.name};
});

#4

Hello,

I am new in WebDev, new in schema, meteor and mongoDB. I want to create a schema that has an autoValue that should load directly from the DB. So I need to convert the DB value into array so that I can access it as an array element for the allowedvalue. I have tried the above but it does not work. Please, advise:
my schema funciton and field:

function getClassification(classification) {

//return db.Classification.find({classification}).toArray();

var sportsArray = Sports.find({classification}).fetch(); // fetch() returns a collection as an array
console.log(sportsArray);
// convert sportArray into an array format autoForm can understand for selection options

var sportsOptions = sportsArray.map( function (obj) {
return {‘label’: obj.name, ‘value’: obj.name};
});

}

priority:{
type: String,
allowedValues: getClassification(priority),
defaultValue: ‘Normal’
},

I get an application error and my app does not restart. What can be the problem here?

Also, I need to read my database value and display them as Tab:

When I pass an array into this component, it work but when i pass my collection it does not work:


                    {this.props.categories.map((category, i) => {     
                       // console.log(i);                 
                        // Return the element. Also pass key     
                        return (<Tab key={i} label={category} />) 
                     })}
                    
               
            </Tabs>

parent element:
import {categories} from ‘…/…/…/…/api/Collections/Categories’; // this my local array
import { Classification } from “…/…/…/…/api/Collections/Collections”; // this is my collection,

export default class Footer extends Component {

render(){

return (
    <div> 
            <footer>
                    <h1> Footer comes Here !</h1>  
                    <TabNavB categories={categories} />
            </footer>

    </div>
       
);

}

}

what is the correct way of doing this?