I am learning the new features available in meteor 1.3 such as the modules and ecmascript package.
My question is, what is the best way to work with imports and exports in regards to where and when to include collections? For example if I am wanting to publish the collection what is the best way to export the collection and make it available to the via exports in order to import it to my publications. ?
Overall if someone has an example or a demo of how they are working with the new modules package that would be great as well.
Iām sorry to sort of hijack this thread, but this is the closest thing to my issue.
Using your answer I can now declare collections without a hitch. However when trying to subscribe to a published collection in a client file, the reference is not defined:
//collections.js
const Test = new Mongo.Collection("test");
if(Meteor.isServer){
Test.insert({name: "Song", createdAt: new Date()});
Test.insert({name: "Sing", createdAt: new Date()});
Test.insert({name: "Sang", createdAt: new Date()});
}
export default Test;
//server.js
import Test from '/lib/collections.js';
const machin = Meteor.publish("test", function(){
Test.find();
});
//client.js
const handle = Meteor.subscribe("test");
console.log(handle);
if(handle.ready()){
const post = Test.findOne();
}
console.log(Test.findOne()); // Result: Test Reference is not defined
(Those are all different files)
Is there some way to import my subscribed collection?
Thank you.
Good evening, and thank you for taking the time.
I tried your solution, however in my server file :
import Test from ā/imports/collections.jsā;
const handle= Meteor.publish(ātestā, function(){
Test.find();
});
console.log(handle); //undefined
</>code>
Handle here returns undefined
and in my client file
import Test from ā/imports/collections.jsā;
It displays nothing in the console (guess itās because of the condition on handle?) but when I type ātestā in the console, it reads as Reference undefined, although the variable is visible.
subscriptions return handles, publish functions do not
youāre still not returning anything from your publish. you need to return Test.find() instead.
Iāve added a more clear example above. Hope this helps!
Yes - when importing/exporting, youāre not exposing your Test variable globally, so it is not accessible through the console.
try using a callback (like in my example above, in the first answer) instead of using the handle, itās a much easier way to test this IMO the callback will only run on success (when the sub is ready) and print them for you!
Hello again,
could I please trouble you to try your method using https://github.com/PeterChauYeg/meteor-react-base ? I forgot to mention that Iām using this as a starter project and Iām beginning to think the problem lies with this repo.
Actually the error thrown by your first code snippet is totally unrelated and different from your provided repo. In your first code snippet, you didnāt import Test on top of your client.js. Thatās why you get the error Test reference is not defined.
For your repo, you got a syntax error in your items.js
console.log(Items.find.fetch())
Note the missing bracket after find. It should be like this
I agree youād gain a lot more from starting with an empty project, and working your way up from there Iāve never used these boilerplates - but I guess they make sense if you want to save time.
Well for one your solution makes it hard to access a collection. When learning Meteor, I learned that once subscribed, a collection could be accessed from any file (provided it ran in the client), your solution makes that yes, data is displayed, but when I try to access it from the console in the browser, I still just get a measly Reference not Defined.
But I think that the issue really stems from the repo, given that any other example Iāve tried works flawlessly. Iāve been going over the starter code but I havenāt been able to find what ticks, and at this point Iām just too frustrated to continue. What I truly needed to begin with was a starter project using React and Material-ui and just enough organized code to get me started.
Iāll follow your advice, take it again from scratch and start on solid foundation this time.
Again, thank you for taking the time.
hehe, I understand the handiness of accessing collections from the console, but this is just not an option with 1.3 if you export your collections - with modules, your not exposing your exports to the global namespace - which means theyāre not accessible in the console but theyāre definitely accessible anywhere - just remember to import dependencies!
1.3 is still in beta for ~a month, so the guidelines for it isnāt in the docs nor the guide yet, but itās comingā¦