Assign return from findOne to a global variable node Node.js

0

I could not do this assignment, is something missing?

 global.pedido = db.collection('configuracoes').findOne({ parametro: "pedido_num"})
    
asked by anonymous 07.01.2018 / 16:11

2 answers

0

You can try putting in a callback function to find an error, if it exists. For example:

try {
  global.pedido = db.collection('configuracoes').findOne({ parametro: "pedido_num"});
} catch (error) {
console.log('>>>>>> Error: ', error);
}
    
07.01.2018 / 16:33
0

findOne returns a Promise , so the result is only ready when it is resolved. The result can be used as follows:

db.collection('configuracoes').findOne({ parametro: "pedido_num"}, function(err, document) {
  if ( !err ) global.pedido = document;
});
    
11.01.2018 / 20:10