Get value from the Mongo field as Variable?

0

Gentlemen, I'm using this code in Meteor:

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

To get the day value in this record:

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

It gives an error, but it rescues the value I want, is there any way to retrieve this value without this error?

    
asked by anonymous 01.02.2018 / 05:51

3 answers

0

The findOne is an asynchronous statement, so the correct way to access the query result is in callback , like this:

Orders.findOne({name : 'Day'}, function(err, day){
    if (!err){
        let teste = day; // aqui a variavel teste recebe o retorno da consulta
    }
});
    
01.02.2018 / 15:05
0

Hand, this funciounou:

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

    
02.02.2018 / 00:05
0

Dude ... for me it works without error. But by syntax, put the field name of your collection in single quotes and the value to be fetched in double quotation marks, like this:

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