Error: A method named '/users/insert' is already defined - mongodb

I've just installed the accounts-google and accounts-ui packages and I'm getting the error:
W20150525-10:41:42.384(1)? (STDERR)
W20150525-10:41:42.384(1)? (STDERR) /home/andy/.meteor/packages/meteor-tool/.1.1.3.4sddkj++os.linux.x86_64+web.browser+web.cordova/mt-os.linux.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:245
W20150525-10:41:42.384(1)? (STDERR) throw(ex);
W20150525-10:41:42.384(1)? (STDERR) ^
W20150525-10:41:42.449(1)? (STDERR) Error: A method named '/users/insert' is already defined
W20150525-10:41:42.449(1)? (STDERR) at packages/ddp/livedata_server.js:1461:1
W20150525-10:41:42.449(1)? (STDERR) at Function._.each._.forEach (packages/underscore/underscore.js:113:1)
W20150525-10:41:42.449(1)? (STDERR) at [object Object]._.extend.methods (packages/ddp/livedata_server.js:1459:1)
W20150525-10:41:42.449(1)? (STDERR) at [object Object].Mongo.Collection._defineMutationMethods (packages/mongo/collection.js:904:1)
W20150525-10:41:42.449(1)? (STDERR) at new Mongo.Collection (packages/mongo/collection.js:209:1)
W20150525-10:41:42.449(1)? (STDERR) at app/meteor.js:1:44
W20150525-10:41:42.449(1)? (STDERR) at app/meteor.js:16:3
W20150525-10:41:42.450(1)? (STDERR) at /home/andy/dev/meteor/.meteor/local/build/programs/server/boot.js:222:10
W20150525-10:41:42.450(1)? (STDERR) at Array.forEach (native)
W20150525-10:41:42.450(1)? (STDERR) at Function._.each._.forEach (/home/andy/.meteor/packages/meteor-tool/.1.1.3.4sddkj++os.linux.x86_64+web.browser+web.cordova/mt-os.linux.x86_64/dev_bundle/server-lib/node_modules/underscore/underscore.js:79:11)
=> Exited with code: 8
My code is currently very simple (very new to this!). Here's meteor.html:
<head>
<title>Hubbub</title>
</head>
<body style="padding-top: 100px;">
<div class="container">
<div class="row">
<div class="col-xs-12">
{{> loginButtons}}
{{> main }}
</div>
</div>
</div>
</body>
<template name="main">
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>age</th>
<th>date</th>
</tr>
</thead>
<tbody>
{{#each users}}
<tr>
<td>{{_id}}</td>
<td>{{name}}</td>
<td>{{age}}</td>
<td>{{date}}</td>
</tr>
{{/each}}
</tbody>
</table>
</template>
and meteor.js:
Users = new Mongo.Collection('users');
if (Meteor.isClient) {
Template.main.helpers({
users: function() {
return Users.find();
},
date: function(){
return moment(this.date).fromNow();
}
});
}
What am I doing wrong here?

Maybe your code is clashing with Meteor default users collection.
You don't need to define a Mongo.Collection to deal with your app users, Meteor comes with its own baked in collection named Meteor.users, use this instead, or rename your collection to something else.

Related

Sails, EJS data is not defined in forEach loop

I am working on Sails.js CRUD application (article management) with MongoDB based on #bradtraversy tutorial. I have encountered issue while trying to display articles from the database using EJS syntax and forEach loop. Error says "articles data is not defined".
Error image
Basically I pass examplary record to Mongo via Sails /Create?title=ArticleOne%20body=BodyOne URL blueprint. The article can be clearly seen in Mongo. MongoDB article image
datastores.js is configured for Mongo:
module.exports.datastores = {
mongodb: {
adapter: 'sails-mongo',
host: 'localhost',
port: 27017,
database: 'articlebase'
},
};
routes.js points out list.ejs view:
module.exports.routes = {
'/': { view: 'pages/homepage' },
'/articles/list': { view: 'pages/list' },
'/articles/add': { view: 'pages/add' },
};
Article.js model:
module.exports = {
attributes: {
title:{
type: 'string'
},
body:{
type: 'string'
}
},
datastore: 'mongodb'
};
ArticlesController.js
module.exports = {
list:function(req, res){
Articles.find({}).exec(function(err, articles){
if(err){
res.send(500, {error: 'Database Error'});
}
res.view('list', {articles:articles});
});
},
add: function(req, res){
res.view('add');
}
};
And finally troublesome list.ejs view:
<h1 class="display-4">Articles</h1>
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Title</th>
<th></th>
</tr>
</thead>
<tbody>
<% articles.forEach(function(article){ %>
<tr>
<td><%= article.id %></td>
<td><%= article.title %></td>
<td>
Edit
<form class="d-inline" action="/articles/delete/<%= article.id %>" method="post">
<input type="submit" value="Delete" class="btn btn-danger">
</form>
</td>
</tr>
<% }); %>
</tbody>
</table>
How can I display articles from MongoDB "articles" collection with EJS correctly? Thanks for your help in advance!
I suspect your controller method is encountering an error, but you are not returning when you get the error. My first guess would be Article vs Articles but I could be wrong. Change to this to help you see what's up:
list:function(req, res){
Articles.find({}).exec(function(err, articles){
if(err){
sails.log('Error summary: ' + err);
sails.log(err);
return res.send(500, {error: 'Database Error'}); // note the return!
}
res.view('list', {articles:articles});
});
},
Otherwise, nice work on the error tracking!
So I was getting this error repeatedly, and my fix was to check the definition of "articles".
If your ejs file is accessing the model "articles" after updating the model using jquery functions, then when you render the ejs file, you can pass the updated "articles" model as a parameter.
Like,
const articles = await Article.find().sort('-entryTime');
res.render('route/ejsfilename', { articles:articles });
This part of the code can be specified in your controller or app.js file(wherever you render your ejs).
In that way when you render the ejs file, you won't get an error of "Cannot read property 'forEach' of undefined" or "articles data is not defined".
go to config/blueprints.js, set
actions: true
blueprints will do auto route for you.

Can not edit the form from sails.js

Hey guys I'm really new at sails.js and I'm using version 1.0 of it. I just tried to create a CRUD application on adding article to my list and showing them. All goes right but when I want to delete or edit it says 404 error. I am going through this tutorial series and as I know this series is using sails js version 0.12.
I am using mongodb as my database as a default. Data insertion is going well but problem is when I wanted to edit or delete it. Here I think the problem is getting the id value from the url. I skipped the edit portion here and posted the delete portion here.
api/controllers/ ArticleController.js
module.exports = {
delete: function(req, res){
Articles.destroy({id:req.params.id}).exec(function(err){
if(err){
res.send(500,'Database Error');
}
res.redirect('/articles/list');
});
return false;
}
};
api/models/ Articles.js
module.exports = {
attributes: {
title:{
type:'string'
},
body:{
type:'string'
}
},
};
views/pages list.ejs
<h1 class="display-4">Articles</h1>
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Title</th>
<th></th>
</tr>
</thead>
<tbody>
<% articles.forEach(function(article){ %>
<tr>
<td><%= article.id %></td>
<td><%= article.title %></td>
<td>
<form class="d-inline" action="/articles/delete/<%= article.id %>" method="POST">
<input type="submit" class="btn btn-danger" value="Delete">
</form>
</td>
</tr>
<% }) %>
</tbody>
</table>
config/route.js
module.exports.routes = {
'/': {
view: 'pages/homepage'
},
};

Not able to bind click event in kendo mvvm row template

I have kendo grid with a button to remove the current item and for that grid I have a row template. Here is my HTML:
<div class="form-horizontal">
<table class="fixed-table-width"
data-role="grid"
data-bind="source: data"
data-scrollable="false"
data-row-template="row-template">
<thead>
<tr>
<th>Item</th>
<th></th>
</tr>
</thead>
</table>
</div>
<script type="text/x-kendo-tmpl" id="row-template">
<tr>
<td><span data-bind="text: item"></span></td>
<td><button class="link" data-bind="click: remove"><i class="icon-trash"></i> Remove</button></br ></td>
</tr>
</script>
And that is my model:
function AddItemComponent($scope) {
if ($scope === null || $scope === undefined) throw new Error("Unknown scope, please provide the scope");
var self = this;
self.itemModel = {
item: "Item to Remove",
remove: function(i) {
self.viewModel.items = self.viewModel.items.splice(i);
}
};
self.viewModel = kendo.observable({
items: []
});
self.viewModel.items.push(self.itemModel);
};
But when I open the modal with this HTML, I get the following error:
kendo.binder.min.js?cdv=40:25 Uncaught TypeError: t.get is not a function(…)
If I remove the data-bind from the click event, there is no error and it just works fine, so what is wrong?
What you are trying to achieve is better done with a commands column
http://docs.telerik.com/kendo-ui/api/javascript/ui/grid#configuration-columns.command
data-columns="[{ field: 'item' }, { command: 'destroy' }]"
You don't neeed a row template anymore.
If you don't want to do this, try to wrap your itemModel in an observable, as get() is a method from kendo.data.ObservableObject
self.itemModel = kendo.observable({
item: "Item to Remove",
remove: function(i) {
self.viewModel.items = self.viewModel.items.splice(i);
}
});

Algolia instantsearch.js Minified exception occurred; invariant

I'm getting an error from instantsearch.js that doesn't make any sense to me. The error is:
Error: Minified exception occurred; use the non-minified dev environment for the full error message and additional helpful warnings.
I've created a fiddle with the code in question:
https://jsfiddle.net/qkqzgsv9/
Here's the HTML:
<div type="text" id="search-box" class="form-control"></div>
<table class="table table-striped">
<thead>
<tr>
<th>Number</th>
<th>Name</th>
<th>Starts</th>
<th>Duration</th>
<th>Room</th>
<th><span class="glyphicon glyphicon-info-sign"></span></th>
</tr>
</thead>
<tbody id="hits-container">
</tbody>
</table>
<div id="pagination-container"></div>
And here's the Javascript:
var search = instantsearch({
appId: '5V0BUFDX8J',
apiKey: 'a25692c12853aea7a77c5a7125498512',
indexName: 'C86FE050-6C48-11E5-84AA-BA5F164D0BA4_events',
urlSync: { useHash: true }
});
search.addWidget(
instantsearch.widgets.searchBox({
container: '#search-box',
autofocus: true,
placeholder: 'Search for events by keyword, description, or event number.'
})
);
search.addWidget(
instantsearch.widgets.hits({
container: '#hits-container',
templates: {
empty: 'No events found',
item: '<tr><td>{{event_number}}</td><td>{{name}}</td><td>{{startdaypart_name}}</td><td>{{duration_name}}</td><td>{{room_name}}</td><td><span class="glyphicon glyphicon-info-sign" data-toggle="tooltip" title="{{description}}"></span></td></tr>'
},
})
);
search.addWidget(
instantsearch.widgets.pagination({
container: '#pagination-container'
})
);
search.start();
It happens in both Safari and Chrome. And I'm not even using the minified version of instantsearch.js. Any idea what's causing it?
This is a tricky bug and it is generated becauses this widget uses React internally.
The widget tries to render a div that will contain your template. But this is incorrect because your template contains td's which can't be rendered in div's, so the browser tries to fix that. This leads to React throwing an invariant violation error because the DOM is not what it expected. And finally you can see this specific error because React is minified in the build.
The fix would be to not use td's or wait for this issue https://github.com/algolia/instantsearch.js/issues/707 to be fixed.

meteor not subscribing to data

I am doing something wrong in my Meteor app but simply cannot find out what it is. I'm not receiving any data in the UI. Here's the code.
lib/common.js
Data = new Meteor.Collection('data2');
'data2' is a collection I created and responds fine in mongo shell
server/app.js
Meteor.publish("Data", function() {
return Data.find({});
});
client/app.js
Meteor.subscribe("Data");
client/helpers.js
Template.home.helpers({
results: function(){
return Data.find();
}
});
I've even tried with specifying the two columns-field1, 2 in find() to no avail.
client/home.html
<template name="home">
<table class="table table-striped tablesorter z-depth-5">
<thead>
<tr>
<th width="15%">field1</th>
<th width="15%">field2</th>
</tr>
</thead>
<tbody>
{{#each results}}
<tr>
<td>{{field1}}</td>
<td>{{field2}}</td>
</tr>
{{/each}}
</tbody>
</table>
Inspection findings -
Data.find().fetch()
[]
Solved:
The mistake I was making was that I was calling the 'local' DB from the c:\data\db directory created when I installed mongodb on my system. But the meteor db is not located there but resides in /.meteor. Upon correction, it works.