Meteor - find return undefined - mongodb

I work on meteor with mongodb. I am trying to get a collection from database. I can insert data without any problem in this collection from meteor, but when I try to find, it doesn't works.
My collection is 'first'.
Server side:
Meteor.publish('first', function(){
return first.find();
});
Client side:
var datacollab = first.find({"Mois":"Mars"});
console.log("collab: " + datacollab);
When I make this command line in mongo shell, it works fine.
I already try to change my request with findOne, or put .fetch at the end.

If you need your code to be in your Template.myTemplate.onRendered hook, then you have several options:
Use a Tracker.autorun that will be automatically re-executed when your DB query / cursor returns new data.
Use the onReady callback of the subscription (assuming you subscribe either when template is created or rendered). Your callback will be executed when the client has received a first full snapshot of the server publication.

Related

Flow Router doesn't work with ObjectID. Any fix?

I'm trying to build routes in my Meteor app. Routing works perfectly fine but getting information from db with route path just doesn't work. I create my page specific routes with this:
FlowRouter.route('/level/:id'...
This route takes me to related template without a problem. Then I want to get some data from database that belong to that page. In my template helpers I get my page's id with this:
var id = FlowRouter.getParam('id');
This gets the ObjectID() but in string format. So I try to find that ObjectID() document in the collection with this:
Levels.findOne({_id: id});
But of course documents doesn't have ObjectIDs in string format (otherwise we wouldn't call it "object"id). Hence, it brings an undefined error. I don't want to deal with creating my own _ids so is there anything I can do about this?
PS: Mongo used to create _ids with plain text. Someting like I would get with _id._str now but all of a sudden, it generates ObjectID(). I don't know why, any ideas?
MongoDB used ObjectIds as _ids by default and Meteor explicitly sets GUID strings by default.
Perhaps you inserted using a meteor shell session in the past and now used a mongo shell/GUI or a meteor mongo prompt to do so, which resulted in ObjectIds being created.
If this happens in a development environment, you could generate the data again.
Otherwise, you could try to generate new _ids for your data using Meteor.uuid().
If you want to use ObjectId as the default for a certain collection, you can specify the idGeneration option to its constructor as 'MONGO'.
If you have the string content of an ObjectId and want to convert it, you can issue
let _id = new Mongo.ObjectID(my23HexCharString);

Is there a way to insert a DB query directly into a new MongoDB on server side?

What I'm trying to do is take a user's query client side (via search function), send it to server via Meteor methods and if there is a search result, insert that query into another MongoDB instance? I need this in order to display search results from the user into client.
Something like this from server side:
UserSearch: function(query){
var init = Table.find({userId: this.userId}, {data: {Name: query}});
Search.insert({userId: this.userId, data: init});
},
Obviously, that doesn't work. But I'm looking for a way to allow to insert a query from one database into a new database.
So I think there are two pieces two your question here:
How do I connect to another MongoDB instance?
How do I insert data from one query result into a collection?
For #1, referencing this SO question, you can do:
var database = new MongoInternals.RemoteCollectionDriver("<mongo url>");
Search = new Mongo.Collection("search", { _driver: database });
For #2, you're simply missing the .fetch() as MasterAM pointed out:
UserSearch: function(query){
var init = Table.find({userId: this.userId, 'data.Name': query}).fetch();
Search.insert({userId: this.userId, data: init});
},
I also modified your Table query a bit as I don't think it's working in the current form. You want each field property in a query object to be in the same object. Note that the fetch will give you an array of Table objects, so you'll need to have your data field in your Search collection reflect that.

Meteor JS : How to get latest set of data based on the date?

I have a requirement where my db has say some set of records with same timestamp (latest) and i want to get all of them at once, i don't want to get any other data which doesn't belong to that criteria, problem is i will not know the timestamp because it stored in db from outside world.
How do i get only the latest set of data in meteor ? i cant do findOne, as it will bring only 1 latest record, which is wrong in my case.
Meteor.publish("collection1", function() {
return Collection1.find({}, {sort:{dateTime: -1}});
});
I tried to do above code but it gets all the record and i think it just sort by desc.
Use the findOne() method to get the latest dateTime value that you can then use as the find() query:
// on the server
Meteor.publish("collection1", function() {
var latest = Collection1.findOne({}, {"sort":{"dateTime": -1}}).dateTime;
return Collection1.find({"dateTime": latest});
});
// on the client
Meteor.subscribe("collection1");
Collection1.find().fetch();
The above approach makes your Meteor app scalable client-side as the client only subscribes to the data that you only need. This is made possible because the return statement inside the Meteor.publish function can only return documents that have the latest dateTime value. This way, you'll avoid overloading the browser's memory no matter how big your server-side database is.

Meteor allow deny functions?

In my application i am using a collection called Polls_Coll
here is my allow function
Polls_Coll.allow({
insert:function(){
return true;
},
update:function(userId, doc, fields, modifier){
return (doc.owner===userId);
},
remove:function(){
return true;
}
});
i inserted a document after log-in to my account
when i try to update that document values from client side
var option_data=Polls_Coll.findOne({_id:this._id}).option2[0].pd;
var u_name=Meteor.user().profile.name;
Polls_Coll.update({_id:this._id,"option2.pd":option_data},{$push:{"option2.$.ids":u_name}});
it is showing error that untrusted code may update using id only but it is working when i try to insert it from server side
Here is how i inserted document into collection
Polls_Coll.insert({question: quest,
option1:[{pd:op1,ids:[]}],
option2:[{pd:op2,ids:[]}],
option3:[{pd:op3,ids:[]}],
option4:[{pd:op4,ids:[]}]
});
why the values are not updating from client side.
From this section in the docs:
Untrusted code includes client-side code such as event handlers and a browser's JavaScript console.
Untrusted code can only modify a single document at once, specified by its _id.
So, I believe, the only selector allowed is an _id. You can achieve the same result with the $set operator with something like:
var poll = Polls_Coll.findOne({_id: this._id});
poll.option2[0].ids.push(Meteor.user().profile.name);
Polls_Coll.update({_id: this._id}, {$set: {option2: poll.option2}});
Meteor handles update() differenty for trusted code ( on the server ) than it does for untrusted code ( on the client ).
Untrusted code can only modify a single document at once, specified by its _id. - from http://docs.meteor.com/#update
Any other criteria inside of the update() selector would allow you to learn what is in the db object by trying multiple selectors, so the development group has eliminated this possibility. If you need to verify that other criteria are met before updating then check this with findOne().

How do you access attributes of an object queried from Mongo in Meteor

I'm pretty new to mongo in the Meteor.js framework. Here, I've queried a MongoDB object using it's ID. I'm trying to access an attribute "mana" but it returns me undefined.
var skill = Skills.find({_id:Session.get("selected_skill")});
alert(skill);//This returns me "undefined"
Skills.update(Session.get("selected_skill"), {$inc: {mana: 1}});
Could you enlighten me on the requirements of accessing attributes in mongo for meteor? Thanks
find method returns a cursor, not object nor array. To access object, you need to either fetch it from the cursor
var skill = Skills.find(Session.get('selected_skill')).fetch()[0];
or get it directly with findOne:
var skill = Skills.findOne(Session.get('selected_skill'));
Then you may use it just as any other js object:
console.log(skill.mana);
skill._cache = {cooldown: true};
Keep in mind that on client-side, collection methods like find are non-blocking. They return whatever Meteor has in cache, not necessarily what is in the server-side db. That's why you should always use them in a reactive context, or ensure that all data has been fetched before execution (don't worry about the latter until you're fluent with Meteor, start with the first way).
Also, you need to keep in mind that because of this, findOne and find.fetch may return null / empty array, even when the corresponding element is in db (but hasn't been yet cached). If you don't take that into account in your reactive functions, you'll run into errors.
Template.article.slug = function() {
var article = Articles.findOne(current_article);
if(!article) return '';
return slugify(article.title);
};
If we didn't escape from the function with if(!article), the expression article.title would raise an error in the first computation, as article would be undefined (assuming it wasn't cached earlier).
When you want to update the database from client side, you can alter only one item by a time, and you must refer to the item by its _id. This is due to security reasons. Your line for this was ok:
Skills.update(Session.get('selected_skill'), {$inc: {mana: 1}});
alert() is a function that returns undefined no matter what you feed it.
alert(42); // -> undefined
Generally, it's far better to debug with console.log than with alert.