igineiz
igineiz

Reputation: 37

Print Object Value from Variable in NodeJS

I want to display object value that i save in variable. How it works in NodeJS?

Here's my code:

const modelMateris = require("../models/materi");

var obj = modelMateris.find().exec((err, result)=>{
  if(result){
     return (result);
  } else (err);
})

console.log(obj);

I got undefined output. So how to print it in NodeJS? Thanks.

Upvotes: 2

Views: 4973

Answers (1)

prabhakaran
prabhakaran

Reputation: 333

if modelMateris.find().exec function returns promise then you can follow the below approach to print the result

var obj = await modelMateris.find().exec();
console.log(obj);    

if promise not supported, then you can print values as below.

modelMateris.find().exec((err, result)=>{
  if(result){
    console.log(result);
  } else (err);
})

Upvotes: 1

Related Questions