1

I'm new to Node so I'm struggling with getting the data from one function to another:

//dhtController.js
const dhtSensor = require('node-dht-sensor').promises;

exports.currentTemperature = async () => {
  const rawTemp = await dhtSensor.read(22, 4); // { temperature: 24, humidity: 60 }

  return rawTemp;
};




//dataController.js
const dhtController = require('./dhtController');

...
exports.getAllReadings = (req, res, next) => {
  const currentTemp = dhtController.currentTemperature(); // undefined

  res.status(200).json({
    status: 'success',
    currentTemp, // undefined
  });
};
...

I tried using async/await in dataController as well - no luck. Old callback-way works, however I rather keep things consistent and use async/await.

Can somebody help me out here? What am I missing? My guess is that in my dataController the results are not in the event loop yet. However 'awaiting' them did not help.

UPD:

Guys, thanks, I tried this from the very beginning:

exports.getAllReadings = async (req, res, next) => {
  const currentTemp = await dhtController.currentTemperature();

  console.log(JSON.stringify({ currentTemp: new Promise(() => {}) }));
  //{"currentTemp":{}}    

  res.status(200).json({
    status: 'success',
    message: 'Here we will have all sensors object',
    currentTemp, // empty
  });
};

Postman result:

{
    "status": "success",
    "message": "Here we will have all sensors object"
}
9
  • 3
    You need to add async keyword in getAllReadings and then use await before dhtController.currentTemperature() Commented Oct 15, 2019 at 15:07
  • Also, currentTemp is not undefined, it is an instance of Promise. If you JSON.stringify({ currentTemp: new Promise(() => {}) }) you get "{\"currentTemp\":{}}", not "{}", so I'm not sure what leads you to think currentTemp is undefined Commented Oct 15, 2019 at 15:09
  • Tried that earlier: exports.getAllReadings = async (req, res, next) => { const currentTemp = await dhtController.currentTemperature(); returns 'undefined' Commented Oct 15, 2019 at 15:11
  • That's not even possible. async functions always return promises, no exceptions. Please produce a minimal reproducible example because I'm certain either you're mistaken, or your example code is not representative of the situation. Commented Oct 15, 2019 at 15:14
  • Is dhtSensor = require('node-dht-sensor') or dhtSensor = require('node-dht-sensor').promises? If it's the former, that's your mistake. Commented Oct 15, 2019 at 15:19

2 Answers 2

3

Since the function you're calling uses an await, you need to make the function you're calling it in async. Which makes sense since you're waiting for a result.

exports.getAllReadings = async (req, res, next) => {
  const currentTemp = await dhtController.currentTemperature(); 

  res.status(200).json({
    status: 'success',
    currentTemp, // undefined
  });
};
Sign up to request clarification or add additional context in comments.

1 Comment

Dude, 10 seconds! 😅
3

Using async and await you can make it work:

exports.getAllReadings = async (req, res, next) => {
  const currentTemp = await dhtController.currentTemperature(); // undefined

  res.status(200).json({
    status: 'success',
    currentTemp, // will be defined
  });
};

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.