2

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.

1
  • Log it inside the callback. Or use a promise that resolves to the result. Commented Aug 20, 2019 at 2:54

1 Answer 1

1

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);
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.