Get Mongo field value as Variable?

Asked

Viewed 378 times

0

Gentlemen, I’m using this code on the Meteor:

var teste = Orders.findOne({name : 'Day'}).day;

To Get the day value on this record:

{_id: "HzoGFKRmYzmH8Yx6A", name: "Day", day: "Jan 27th 18"}

It gives an error, but redeems the amount I want, is there any way to redeem this value without this error?

  • Where are you running this (var teste = Orders.findOne({name : 'Day'}).day;) command, server-side or client-side?

3 answers

0

Bro, this has worked:

Orders.find({name : 'Day'}). fetch(). foreach(Function(day){ var day = day.day; console.log(day);

0

Man... it works like a charm for me. But by way of syntax, put the name of the field of your collection between single quotes and the value to be searched in double quotes, like this:

let teste = Orders.findOne({'name': "Day"}).day;
console.log(teste);

0

The findOne is an asynchronous instruction, so the correct way to access the result of the query is in callback, thus:

Orders.findOne({name : 'Day'}, function(err, day){
    if (!err){
        let teste = day; // aqui a variavel teste recebe o retorno da consulta
    }
});
  • bro, thanks for the answer, but keep giving error, was this error here: Exception in template helper: Error: Match error: Failed Match.Oneof, Match.Maybe or Match.Optional validation at Exports.check (http://localhost:3000/Packages/check.js? hash=eab389b8683d2f0b47a9d511894c2082fc6155a9:65:15) at Mongo.Collection. _getFindOptions (http://localhost:3000/Packages/Mongo.js? hash=337de4ed4979262aa9b84177742e90e72b1e1c:344:7)

  • is using Media? this seems to be another problem..

  • eh, I’m using Meteor, that instruction is inside a helper.... your answer seems right even and should work.... strange ne....

  • At a glance in that post

  • I looked around, but I can’t find the problem.... is all subscribed and published correctly, as I use other similar instructions in other files, ta bound...

  • this is not an error : Orders.find({name : 'Day'},{limit : 1 }, Function(err, day) but does not execute the function I put.... I was seeing that it relates something to findOne

  • Sorry, I believe I gave an answer to assuming you were using Mongoose. try var teste = Orders.find({name : 'Day'},{limit : 1 }) only. Then on the bottom line display the test value.

  • this works without error, but returns me the cursor (the whole record), I only want the field day, which is inserted in this record.... I have too much pleasure in your availability my brother

  • as shown in this record: {_id: "Hzogfkrmyzmh8yx6a", name: "Day", day: "Jan 27th 18"}

  • var teste = Orders.find({name : 'Day'},{limit : 1, fields: {'day':1} }) try so Neto

  • mano, sure, but it returns the cursor, the Json record. I wanted it to return me only the value of field.... The problem is that in the console in the right browser, but in the Content does not work.... even if I deny the appearance of the other fields, it continues to return as Json

  • look at this one, it was supposed to work, but from Undefined: var test = Orders.find({name : 'Day'}, {limit : 1}). fetch(); var teste2 = test.day; console.log(teste2);

  • What stops you from doing that?

  • because I will use the value acquired as search for another find, then it will not accept this way....

  • Can you test that: var teste = Object.values(Orders.find({name : 'Day'},{limit : 1, fields: {'day':1} }))[0];

  • also did not give, returns cursor.... think old kkkkkkkkkk

  • Show me the shape you’ll use on another find

  • Orders.find({orderDate: test, userid: Meteor.userid()}). fetch();

  • Man, have you tried Orders.find({orderDate: teste['day'], userId: Meteor.userId()}).fetch();?

  • nothing, this is weird dude, it was supposed to work...but the console keeps giving Undefined and not working.... this result comes from a prompt asking the user the day of the order he wants to see.... Would you know another way to pass the information from Vents to the helper, without having to insert it in db ??? because I’m doing this, I insert into the bank with Vents, then rescue with the helper

Show 15 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.