Meteor App - 3 Rows from MongoDB is not displaying on Browser - mongodb

What wrong with this code?
The 3 rows from mongoDB wont display on browser.
Please help.
My Short Meteor Code
Really cant find what's wrong.
<head>
<title>Things to do.</title>
</head>
<body>
<div class="container">
<header>
<h1>Our List</h1>
</header>
<ul>
{{#each tasks}}
{{> task}}
{{/each}}
</ul>
</div>
</body>
<template name="task">
<li>{{text}}</li>
</template>
Tasks = new Mongo.Collection("tasks");
if (Meteor.isClient) {
// This code only runs on the client
Template.body.helpers({
tasks: function () {
return Tasks.find({});
}
});
}
db.tasks.find()
2015-08-14T08:27:44.644+0000 trying reconnect to 127.0.0.1:8081 (127.0.0.1) failed
2015-08-14T08:27:44.644+0000 reconnect 127.0.0.1:8081 (127.0.0.1) ok
{ "_id" : ObjectId("55cd9d2456b678da6dcaa972"), "text" : "Hello world!", "createdAt" : ISODate("2015-08-14T07:47:48.586Z") }
{ "_id" : ObjectId("55cd9e2b56b678da6dcaa975"), "text" : "Eat out!", "createdAt" : ISODate("2015-08-14T07:52:11.635Z") }
{ "_id" : ObjectId("55cd9e3e56b678da6dcaa976"), "text" : "Tour around the world.", "createdAt" : ISODate("2015-08-14T07:52:30.944Z") }
There it is, my code in pure text.

A few things that could go wrong:
Your Tasks collection may not be published. Make sure you still have the autopublish package installed in your app, or publish and subscribe to your collection following this tutorial.
You are not using the same Mongodb database as your app's: make sure by printing the app's MONGO_URL.
In your js file:
if (Meteor.isServer) {
console.log(process.env.MONGO_URL);
}

Related

Simple search function in meteor using EasySearch package

Good Day,
I'm trying to create a simple search function using the easy search package.
In a nutshell I have done the following-
Defined a schema and index on the client as such:
const Patients = new Mongo.Collection('patients');
const PatientsIndex = new EasySearch.Index({
collection: Patients,
fields: ['name'],
engine: new EasySearch.MongoDB()
});
I've entered the following values into the database:
meteor:PRIMARY> db.patients.find()
{ "_id" : ObjectId("57d6a9f71bad26ba07748f9d"), "name" : "Paul" }
Created a template helper on the client side:
Template.searchBox.helpers({
patientsIndex: () => PatientsIndex
});
And lastly I've created a template which should output the results:
<template name="searchBox">
{{> EasySearch.Input index=patientsIndex }}
<ul>
{{#EasySearch.Each index=patientsIndex }}
<li>Name of the patient: {{name}}</li>
{{/EasySearch.Each}}
</ul>
</template>
Now for some reason this just wont work, it renders nothing to the template, I' very new to this and would really appreciate some assistance.
Thanking you.
From your code samples it looks like you're trying to refer to both Patients and PatientsIndex globally. Assuming you have your Patients and PatientsIndex declarations in a shared client/server location (like /lib), then you should remove the const keyword. That will make sure these declarations are available globally, and will allow your Template to use them. Here's a modified version of your code that will work:
/lib/collection.js
Patients = new Mongo.Collection('patients');
PatientsIndex = new EasySearch.Index({
collection: Patients,
fields: ['name'],
engine: new EasySearch.MongoDB()
});
/client/main.html
<body>
{{> searchBox}}
</body>
<template name="searchBox">
{{> EasySearch.Input index=patientsIndex }}
<ul>
{{#EasySearch.Each index=patientsIndex }}
<li>Name of the patient: {{name}}</li>
{{/EasySearch.Each}}
</ul>
</template>
/client/main.js
import { Template } from 'meteor/templating';
import './main.html';
Template.searchBox.helpers({
patientsIndex() {
return PatientsIndex;
}
});

Meteor Connection

I'm trying to connect my app with mongodb. Adding a post with static array data works fine.
Problem: Nothing shows now up under {{post}}. If I check my DB, there is already data inserted.
Database Insert + Code:
db.calposts.insert({ post: "Hello world!", createdAt: new Date() });
WriteResult({ "nInserted" : 1 })
{{#each calposts}}
{{> posts}}
{{/each}}
<template name="posts">
<div class="panel-post" id="post-draggable">
<span>{{post}}</span>
</div>
</template>
if (Meteor.isClient) {
Template.calendar.helpers({
calposts: function () {
return CalPosts.find({});
}
});
}
Did you publish calpost collection to the client?
If not, in the server folder somewhere you need to use Meteor.publish() and then on the client side, run Meteor.subscribe() to subscribe to the publication.
Check out this page on meteor.com for more info on publishing and subscribing:
https://www.meteor.com/tutorials/blaze/publish-and-subscribe

Retrieving and displaying MongoDB document content in Laravel 4 with Jenssegers Laravel library

I am new to Laravel. I am using the Jenssegers Laravel library, an eloquent model and query builder with support for MongoDB.
In my database, I created the document below. I am trying to show on the browser the content of the document but I am not successful.
I'd appreciate some hints or help on how to retrieve and display the content of a MongoDB document on Laravel 4.
Thanks.
{
"_id" : ObjectId("537124d584142189174ce113"),
"username" : "usertest",
"password" : "passtest",
"email" : "test#email.com",
"school" : "College university",
"country" : "USA",
"state" : "Washington",
"city" : "Seattle"
}
This is the code I got down so far..
File: /app/models/User.php
<?php
use Jenssegers\Mongodb\Model as Eloquent;
class User extends Eloquent {
/**
* The database table (collection) used by the model.
*
* #var string
*/
protected $collection = 'user';
$users = User::all();
public function all()
{
return $this->$users;
}
}
File: /app/routes.php
Route::get('users', function()
{
return View::make('users')->with('user',$users);
});
File: /app/views/users.blade.php
#extends('layout')
#section('content')
#foreach($users as $user)
<p>{{ $user->name }}</p>
#endforeach
#stop
File: /app/views/layout.blade.php
<html>
<body>
<h1>Laravel Quickstart</h1>
#yield('content')
</body>
</html>
maybe this is obvious, but if you wan to use users variable in view:
#foreach($users as $user)
// some code
#endforeach
then in route (or controller) you should you should pass users not user:
// should be
return View::make('users')->with('users',$users);
// not
return View::make('users')->with('user',$users);
Also I would rather put User::all() in route/controller then in model, because like mentioned in comment by Muharrem Tigdemir - it is already declared in Eloquent. It would look something like this:
Route::get('users', function()
{
return View::make('users')->with('user', User::all());
});
Hope it helped.
In your document you have username, but in the users.blade.php file you are referencing <p>{{ $user->name }}</p>
It should be <p>{{ $user->username }}</p>

angular error : object has no method push

I am trying to make a angular work with a REST service and $resource.
it works to GET the datas from JSON, but when updating whith $save() or a custom method called $rec(), i have an error in the console saying : TypeError: Object #<c> has no method 'push'. Switching isArray to true or false, didn't change anything.
i made a plunker : http://plnkr.co/edit/fS2bjRKPgUulTbTxgg2j
the error is visible when you make it run on your own server.
the html:
<div ng-controller="TestCtrl">
<div ng-repeat="obj in objs">
<div>
<h3>{{obj.name}}</h3>
<h3>{{obj.age}}</h3>
<input type="text" ng-model="obj.name" /><br />
<input type="text" ng-model="obj.age" /><br />
<button ng-click="save(obj)">Save</button>
</div>
</div>
</div>
and the javascript:
'use strict';
angular.module('TestApp', ['TestApp.services', 'TestApp.controllers']);
angular.module('TestApp.services', ['ngResource']).
factory('Obj', function($resource){
return $resource('datas.json', {}, { 'rec': {method: 'POST', isArray: true } });
});
angular.module('TestApp.controllers', []).
controller('TestCtrl', ['$scope', 'Obj', function($scope, Obj) {
$scope.objs = Obj.query();
$scope.save = function(obj) {
obj.$save();
console.log(obj);
}
}]);
do you know where the error comes from ?
I have copied your plnkr to my dev machine, set up a sinatra site as below but I still do not see this error. the client posts a JSON string like {"name"=>"louise", "age"=>"32"} when I click on a blue button, and everything seems fine. Are you handling the POST appropriately?
here is my server code:
app.ru:
# -*- coding:utf-8; mode:ruby; -*-
require 'json'
require 'multi_json'
require 'sinatra/base'
require 'sinatra/json'
class App < Sinatra::Base
helpers Sinatra::JSON
get '/datas.json' do
json([
{"name" => "louise", "age" => "32"},
{"name" => "jeanne", "age" => "25"},
{"name" => "renée", "age" => "21"},
{"name" => "fernande", "age" => "28"}
])
end
post '/datas.json' do
data = JSON.parse request.body.read.to_s
200
end
end
config.ru:
# -*- coding:utf-8; mode:ruby; -*-
require './app.rb'
run App

How to select a specific node programmatically?

I have a jstree. I want to select the node which is bound to the object which has a location with id of 158. This works but seems stupid. What's the more idiomatic way of doing this?
var $tree = $('.jstree', myContext),
node = $tree.find('li').filter(function() {
return ( $(this).data().location || {}).id === 158;
});
$tree.jstree('select_node', n)
Just wanted to chime in here as none of the answers worked for me. What finally DID work was very simple:
$('#someTree').jstree('select_node', 'someNodeId');
Note that I didn't initialize someNodeId as a jQuery object. It's just a plain string.
I did this right after a tree was loaded without putting it into a "ready" bind event as it seems to not be necessary.
Hope it saves some one from a few frustrating hours. . .
To hook into the tree after it has been loaded:
.on('loaded.jstree', function() {
// Do something here...
});
Based on jsTree groups you can try
jQuery("#getStartedTree").jstree("select_node", "#test2");
if the data looks like
The JSON in the TextFile.txt - borrowed from your simple example
[
{
"data" : "A node",
"children" : [ "Child 1", "Child 2" ]
},
{
"attr" : { "id" : "test1" },
"data" : {
"title" : "Long format demo",
"attr" : { "id" : "test2", "href" : "#" }
}
}
]
and jsTree set up
My tree container is <div id="getStartedTree">
My jsTree code
$("#getStartedTree").jstree({
"themes": {
"theme": "default",
"url": "../App_Css/Themes/Default/style.css",
"dots": true,
"icons": true
},
"json_data": {
"ajax": {
"url": "../SiteMaps/TextFile.txt",
"dataType": "json",
"data": function(n) {
return { id: n.attr ? n.attr("id") : 0 };
}
}
},
"plugins": ["themes", "json_data", "ui"]
});
Is that what you are after?
I did it with:
$('.jstree').jstree(true).select_node('element id');
this code:
jQuery.each(produto.categorias, function(i, categoria) {
$('#lista-categorias').jstree(true).select_node(categoria.dadoCategoria.id);
});
I was able to simulate a click on a jstree node as an alternative way to select a node.
The following code is what was used :
$(treeIdHandle + " li[id=" + nodeId + "] a").click();
If you're populating the tree using HTML instead of JSON data and wondering how to set the node_id, you just need to set the id attribute of the <li> element!
<div class="tree-menu">
<ul class="menu">
<li id="node_1">
Node 1 - Level 1
<ul class="menu">
<li id="node_3">
Node 3 - Level 2
</li>
</ul>
</li>
<li id="node_2">
Node 2 - Level 1
</li>
</ul>
</div>
Then
$('.tree-menu')
.jstree(true)
.select_node("node_3");
will select the Node 3 - Level 2 node.
For those getting javascript errors, remember to use Full version of jQuery, not the slim version!
For all down voters, here is the demo to prove it's working:
https://jsfiddle.net/davidliang2008/75v3fLbs/7/
i use jstree 3.0.8. don't use 'state'
'plugins' : ['dnd','sort','types','contextmenu','wholerow','ui']
and server offer the json, the selected node has
"state":{"selected":true,"opened":true}
This solution Works for me
// after the tree is loaded
$(".jstree").on("loaded.jstree", function(){
// don't use "#" for ID
$('.jstree').jstree(true).select_node('ElementId');
});
and even in a php loop (dynamically) :
$(".jstree").on("loaded.jstree", function(){
<?php foreach($tree as $node): ?>
$('.jstree').jstree(true).select_node('<?=$node?>');
<?php endforeach;?>
});
Hope this works for you.
i think u should write code to select node after jstree initialized, therefore use this code
$('#jstree')
.on('ready.jstree', function (e, data) {
// do function after jstree initialized
$('#jstree')
.jstree(true)
.select_node('nodeid');
});
hope its work :)
trigger click on first anchor
$("#jstree .jstree-anchor:first").click();
or by node id 158
$("#jstree #158").find(".jstree-anchor:first").click();
$('#' + 158).find(".jstree-anchor:first").click();