I'm discovering moteor.js, and I struggle with the basics. So far I just followed the tutorial, and everything is fine until I get to the Collections part. My app is running, mongod is running, but for some reason, the app crashes on this line
Tasks = new Mongo.Collection("tasks");
if (Meteor.isClient) {
Template.hello.greeting = function () {
return "Welcome to hannibal.";
};
Template.hello.events({
'click input' : function () {
if (typeof console !== 'undefined')
console.log("You pressed the button");
}
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
});
}
When I open the page in localhost:3000, I get this message
ReferenceError: Mongo is not defined
at app/hannibal.js:1:28
Any idea why is it doing this ? I thought it might come from the fact that I'm using meteor for Windows.
In >v0.9.1, if you're using Mongo in a custom package, check that package.js includes api.use('mongo', [client, server]) inside onUse. See this related question.
It sounds like you have an old version of Meteor installed.
Run meteor update from the command line.
What worked for me was directly importing Mongo from Meteor as so:
import { Mongo } from 'meteor/mongo'
Related
Has somebody found a way to check if an index has been created after calling _ensureIndex / createIndex without using the Mongo Shell but in Meteor server code?
I am writing a package test, where I want to assert, that the indices have been created during some package code execution.
I'm using this code to extend collection prototype for getting indexes synchronously:
getIndexes.js:
const Future = Npm.require('fibers/future');
Mongo.Collection.prototype.getIndexes = function() {
const raw = this.rawCollection();
const future = new Future();
raw.indexes(function(err, res) {
if(err) {
future.throw(err);
}
future.return(indexes);
});
return future.wait();
};
When I try to run something as simple as this, I get an error: 'static() root path required'.
If only one 'it' is run, it will pass.
Anyone knows what's the catch?
var Sails = require('sails');
describe("Crud tests:", function() {
var app;
beforeEach(function(done) {
// Lift Sails and start the server
Sails.lift({
log: {
level: 'error'
},
}, function(err, sails) {
console.log("sails lifted");
app = sails;
done(err, sails);
});
});
afterEach(function(done) {
Sails.lower(done);
console.log('sails down');
});
it("1", function(done) {
expect(1).toEqual(1);
done();
});
it("2", function (done) {
expect(2).toEqual(2);
done();
});
});
See https://github.com/balderdashy/sails/issues/1860, quoted below:
Looking at the core tests, even in the ones where we lift / lower for
each individual test, it's always with a fresh instance of Sails. I
don't think a lot of testing has gone into lowering / re-lifting the
same instance, and I wouldn't be shocked to find out that some globals
are hanging around that screw up the lift sequence. So unless there's
a reason why you need it to be the same Sails, rather than a new Sails
with the same options, I'd follow the example of the core tests and
create a fresh instance. To do so, you require the Sails factory,
instead of the full Sails module:
var Sails = require('Sails/lib/app')
var sailsInstance = new Sails();
sailsInstance.lift(...);
I think that the sails v0.10 should be lifted differently. The code bellow is from my project which runs on rc9.
# test/support/sails.coffee
process.env.NODE_ENV = 'test'
process.env.PORT = 1338
Sails = require('sails/lib/app')
app = Sails()
beforeEach (done) ->
app.lift
models:
migrate: 'drop' # rebuild database (optional)
, done
afterEach (done) ->
app.lower done
describe ...
I hope it helps.
I have a simple Meteor/MongoDB project using the 'roles' package where I optain data from the db to the client. The roles package seems to work fine and the browser shows the right data depending on who is logged in, just like it should do. Then when running 'meteor remove autopublish' in the terminal inside my applications directory I get 'autopublish removed' just like it should. Still I can retrieve data from the server just as before(!?)
I have all of my db calls from the client/client.js.
The server/server.js does nothing (I do have publish/subscribe code but uncomment for now) and same goes for the common js file in main directory.
How can this be? Am I perhaps retrieving data from minimongo somehow? I have also removed insecure even if I don't think that matters in this case(?) Thanks in advance.
EDIT: Here's the code:
client.js:
//when uncomment the subscribe's you should not get access to the server/db, but 'data' that holds all the inlogg info still shows. The 'movies' on the other hand doesn't, just like it shouldn't.
//Meteor.subscribe('data');
//Meteor.subscribe('movies');
/*############# Get User Data ###############*/
Template.userLoggedIn.id = function () {
return Meteor.userId();
};
Template.userLoggedIn.email = function () {
var email = Meteor.users.findOne({_id: Meteor.userId()});
return email.emails[0].address;
};
Template.userLoggedIn.profile = function () {
var profile = Meteor.users.findOne({_id: Meteor.userId()});
return profile.profile.name;
};
Template.userLoggedIn.role = function () {
var role = Meteor.users.findOne({_id: Meteor.userId()});
return role.roles[0];
};
/*############# ###############*/
Template.movies.movies = function() {
var movies = Movies.find().fetch();
return movies;
}
server.js:
Meteor.publish('data', function () {
return Meteor.users.find();
});
Meteor.publish('movies', function() {
return Movies.find();
});
Thanks for providing the code - I see how this could be confusing. The users section of the docs should be written to explicitly say this, but what's happening is the current user is always published. So even if you don't write a publish function for users (or your have your subscribe commented out), you should expect to see the current user on the client. Because your template code only looks for Meteor.userId(), I would expect it to still work.
Assuming you have other users in the database, you can quickly check that they are not being published by running: Meteor.users.find().count() in your browser console. If it returns 1 then you are only publishing the current user (or 0 if you are logged out).
I kept having this error when i deploy my app onto meteor cloud server.
Meteor code must always run within a Fiber
at _.extend.get (app/packages/meteor/dynamics_nodejs.js:14:13)
at _.extend.apply (app/packages/livedata/livedata_server.js:1268:57)
at _.extend.call (app/packages/livedata/livedata_server.js:1229:17)
at Meteor.startup.Meteor.methods.streamTwit (app/server/server.js:50:24)
however, I have already wrapped within Fibers
streamTwit: function (twit){
var userid = '1527228696';
twit.stream(
'statuses/filter',
{ follow: userid},
function(stream) {
stream.on('data', function(tweet) {
Fiber(function(){
if(tweet.user.id_str === userid)
{
Meteor.call('addQn', tweet);
}
}).run();
console.log(tweet);
console.log('---------------------------------------------------------');
console.log(tweet.user.screen_name);
console.log(tweet.user.name);
console.log(tweet.text);
});
}
);
}
I don't know what's the reason but someone suggested that i should wrap it with Meteor.bindEnvironment instead. Hence, I did this:
streamTwit: function (twit){
this.unblock(); // this doesn't seem to work
console.log('... ... trackTweets');
var _this = this;
var userid = '1527228696';
twit.stream(
'statuses/filter',
{ follow: userid},
function(stream) {
stream.on('data', function(tweet) {
Meteor.bindEnvironment(function () {
if(tweet.user.id_str === userid)
{
Meteor.call('addQn', tweet);
}
}, function(e) {
Meteor._debug("Exception from connection close callback:", e);
});
console.log(tweet);
console.log('---------------------------------------------------------');
console.log(tweet.user.screen_name);
console.log(tweet.user.name);
console.log(tweet.text);
});
}
);
}
//add question method
addQn:function(tweet){
questionDB.insert({'tweet': tweet, 'date': new Date()});
}
but now it doesn't even work. I realise that this only happened when I tried to insert some data into mongodb.
May I know what is the problem with my code? Thanks!
All these codes were written in app/server/server.js
You shouldn't need to use Meteor.call on the server side. That is for client-side code only. Just call addQn directly or better yet, inline it since it's just one line of code.
I am using MongoDB with Codeigniter (Cimongo) and I need to print out the results from
the command db.currentOp on a webpage so that I can use the data to debug.
How can I do this?
Thankful for all help!
Based on Viewing and Terminating Current Operation from the MongoDB docs, the db.currentOp() command is simply a query against the special $cmd.sys.inprog collection of a database. You can also confirm this via the JS shell:
$ mongo
MongoDB shell version: 2.1.0
connecting to: test
> db.currentOp
function (arg) {
var q = {};
if (arg) {
if (typeof arg == "object") {
Object.extend(q, arg);
} else if (arg) {
q.$all = true;
}
}
return this.$cmd.sys.inprog.findOne(q);
}
I haven't worked with CodeIgniter or Cimongo, but looking at Cimongo.php, you should be able to use the get() method with $cmd.sys.inprog to receive a cursor, which you can then use to read the first element. There doesn't appear to be any abstraction for MongoCollection::findOne(), but that would have been my first choice for doing this in raw PHP:
$mongo = new Mongo();
$inprog = $mongo->selectCollection('test', '$cmd.sys.inprog');
var_dump($inprog->findOne());