0

This has been asked before but none of the answers were understandable by myself.

Using Meteor, I am pulling in a collection called Scrdata into a Select/Option. That works fine. Inside of the Option I am inserting the _id of that document. It all works fine.

<select class="selectfile">
<option>Click to Select</option>
{{#each cwCasesPending}}
<option value="{{_id}}">{{> allcase}}</option>
{{/each}}
</select>

<template name="allcase">
<div > {{last_name}}, {{first_name}}: {{facility}} - {{cwname}}</div>
</template>

I am attempting to do what should be an extremely simple matter, find one document by its id:

Template.caselist.events({
'change .selectfile': function(event, tmpl){
var ar = Scrdata.findOne({_id: $(event.target).val()});
console.log($(event.target).find('option:selected').val());
console.log(ar);
}
});

The Results in the console are:

ObjectID("56dab90a73176cc2deb25aaa")
undefined

My goal is, upon a change in the select block, to populate a form on that page. However, a find by _id does not work.

I am grateful for any insight and guidance. Thank you.

Lawyer_Coder
  • 203
  • 1
  • 3
  • 14
  • The console log does not have the same base info. With findOne you are just using `$(event.target).val()` instead of `$(event.target).find('option:selected').val()`. Try once just the usage of a known _id like `var ar = Scrdata.findOne({_id: "known_id"});` – Tom Freudenberg Mar 06 '16 at 14:23
  • @user2690440 It looks like you are using ObjectIDs, so you may need to [convert them](https://stackoverflow.com/questions/29462623/convert-string-to-mongo-objectid-in-javascript-meteor) before the call to `findOne`. – David Weldon Mar 06 '16 at 14:52

1 Answers1

0

The basic idea in your occassion is to pull a document from the database by using the id as a filter. If the code above does not work, you could try several ways to fix it:

First way is to try:

var id = $(event.target).find('option:selected').val();
var ar = Scrdata.findOne({_id: id});

If this does not work obviously the $(event.target).find('option:selected').val() returns some object. Print this object on the console. See its properties (at least one of them is the id if the printed code above is what you receive by now). And then again use:

var id = $(event.target).find('option:selected').val().(the property of the object);

I use this way when I face similar problems. Hope it will work for you. See this question:

Community
  • 1
  • 1
StefanL19
  • 1,476
  • 2
  • 14
  • 29
  • If I console.log($(event.target).find('option:selected').val()), I get undefined. I also notice that when I do a Scrdata.find().fetch() I get an Array of 3,000 objects – Lawyer_Coder Mar 06 '16 at 14:45
  • did you try with $(this).find('option:selected').val(). I am editing my answer to link you to another question but I think that this should solve your problem. – StefanL19 Mar 06 '16 at 14:52
  • If I place this in the event: 'change #selectfileNew': function(event){ var id = $(event.target).find('option:selected').val(); console.log(id); var ar = Scrdata.findOne({_id: id}); console.log(ar); } I get in the console ObjectID("56dab90a73176cc2deb25af5") and undefined. Might my issue be that I am deriving the value from – Lawyer_Coder Mar 06 '16 at 15:05
  • I believe I have identified the problem. I replaced: – Lawyer_Coder Mar 06 '16 at 15:18
  • @user2690440 Using vanilla Meteor, this should not be necessary, as Mongo IDs will be stored as Strings, not Object IDs. Are you using a pre-existing database or adding documents from the console? – Autumn Leonard Mar 07 '16 at 14:12