[SOLVED] Help using mongoDB

Im learning about mongo database, i have this code on my main.html

<body>
	<ul>
		{{#each resolutions}}
			{{>prueba}}
		{{/each}}
	</ul>
 </body>

<template name="prueba">
  <li>{{title}}</li>
</template>

and this one in my main.js

import { Template } from 'meteor/templating';
import './main.html';
Resolutions = new Mongo.Collection('resolutions');

Template.body.helpers({
	resolutions: function() {
		return Resolutions.find();
	}
});

I want my page to show the {{title}} elements (in this case just “show me”) inserted to Resolutions in cmd by

meteor mongo
db.resolutions.insert( { title: "show me" });

I used the insert method while my app was running but the page its still blank. I know this is a beginer question but I have been trying like an hour (just this part) changing paths, importing Resolutions db and any stuff that I came up with but none of them worked so i would really apreciate help in this one (and if you want, a brief explication of mongo databases behaviour).

Your#each block creates a new data context on every iteration and that context is a document that was returned from the cursor. Your prueba template though has it’s own context and doesn’t know about the parent context. You’ll need to pass that context to the template inclusion tag as the first param for your code to work.

<body>
	<ul>
		{{#each resolutions}}
			{{>prueba this}}
		{{/each}}
	</ul>
 </body>

<template name="prueba">
  <li>{{title}}</li>
</template>

Blaze also has an #each in block which can be used so that your intent is more clear.

<body>
	<ul>
		{{#each resolution in resolutions}}
			{{>prueba resolution}}
		{{/each}}
	</ul>
 </body>

<template name="prueba">
  <li>{{title}}</li>
</template>
1 Like

I tried both ways but still cant make it work :frowning:
I tryied typing Resolutions (to check if it exists at least) from my app directory but it say “Resolutions is not defined” , maybe …/client/main.js is the wrong place to create the collection… where should i create it?

I testing. and for me it’s good.

What your version of meteor ?

My code …

/client/main.html

<head>
  <title> TEST </title>

</head>
<body>
 <h1>TEST</h1>

 <ul class="collection">
   {{#each resolutions}}
       {{> resolution}}
   {{/each}}
     </ul>

 </body>

<template name="resolution">
  <li>{{title}}</li>
</template>

/client/main.js

import { Template } from 'meteor/templating';
import {Resolutions} from '../lib/resolutions'

import './main.html';

Template.body.helpers({
//resolutions : [{title:'show me'}]
resolutions() {  return Resolutions.find({});}
});

/lib/resolutions.js

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

export const Resolutions = new Mongo.Collection('resolutions');

Looking forward to reading you

meteor 1.9.0 (and meteor-base 1.4.0),
I tried creating the lib directory with your code and importing from client/main.js but it didn’t work , then I replaced all my files code with yours, still nothing , is it something with my PC? (tried using meteor mongo from lib but nothing :frowning: )

It finally got “fixed”, the problem was that the db ‘resolutions’ wasn’t loaded on server/main.js.
Now I have to find how to reset the collection as it’s filled with my previous tests.
Thank you guys!