0

I am successfully sending my data to other file but now it returns undefined, when I console log the data i want to return.. seems like that my data printed after the undefined

if (req.body.act === "post") {
    // console.log(req.body.img)
    var imgLabels
    if(req.body.img){
      
      console.log("here is the file called");
    
      imgLabels = imageReko(req.body.img)
      
      console.log("here is the data after return function");
      console.log(imgLabels);
    }

output

here is the file called
here is the data after return function
undefined
POST /posts 200 228.708 ms - 14
Detected labels for: aa40ea657545cf35374919fc78da9cd9
the data in side the file
[
  {
    Name: 'Text',
    Confidence: 99.82980346679688,
    Instances: [],
    Parents: []
  },
  {
    Name: 'Handwriting',
    Confidence: 95.81718444824219,
    Instances: [],
    Parents: [ [Object] ]
  },
  {
    Name: 'Lighting',
    Confidence: 87.46612548828125,
    Instances: [],
    Parents: []
  }

so I want to wait the data to be returned but idk how to do that ? please can u write a code when explain it to me

i edited the code sorry

as comments say they want imageReko code here it is

// Load the SDK and UUID
var AWS = require('aws-sdk');
var uuid = require('node-uuid');


const imageReko = (img) =>{
    const bucket = 'image-for-reko1' // the bucketname without s3://
    
    var string = img.split("/");
    // console.log(string);
    // console.log(string[2]);
    
    const photo  = string[2] // the name of file
    // console.log(img);
    // console.log("this is imgreko");
    const config = new AWS.Config({
        accessKeyId: process.env.AWS_ACCESS_KEY_ID,
        secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
        region: process.env.AWS_REGION
    }) 


    AWS.config.update({
    region: 'eu-west-2'
    });
    // console.log(config)


    const client = new AWS.Rekognition();
    const params = {
        Image: {
            S3Object: {
            Bucket: bucket,
            Name: photo
            },
        },
        MaxLabels: 3,
        MinConfidence: 85
    }
    client.detectLabels(params, function(err, response) {
        if (err) {
            console.log(err, err.stack); // an error occurred
        } else {
            console.log(`Detected labels for: ${photo}`)
            //response.Labels.forEach(label => {
           
            //}) // for response.labels
            console.log("the data in side the file");
            console.log(response.Labels);
            return response.Labels
        } // if
    });
}
module.exports = imageReko;

edit new things appended

i changed the code to

if(req.body.img){
      
      console.log("here is the file called");
      var fun = async function test()
      {
        return await imageReko(req.body.img)
      }
      imgLabels =  fun() 
      console.log("here is the data after return function");
      console.log(imgLabels);
    }

output : enter image description here how do i know the value returned ? cas i wana put this value in my database

  • use async and await – Jatin Mehrotra Jun 09 '21 at 16:50
  • I tried but it dont return data instead – Zeyad Alaa Eldin Jun 09 '21 at 16:53
  • Please show the code for `imageReko()`. It is presumably asynchronous and thus it returns long before it is complete. To help you work with that, we need to see the code. For general purpose calling of asynchronous functions see [How to return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call). – jfriend00 Jun 09 '21 at 16:53
  • okay i edited the post .. now i will go see the link that u provided – Zeyad Alaa Eldin Jun 09 '21 at 16:59
  • https://ideone.com/maFOFv so as described in that link you send, that Synchronous will help me to wait untill this var is filled with something. am i right ? if yes then this still giving me undefined – Zeyad Alaa Eldin Jun 09 '21 at 17:12

1 Answers1

0

i solved it ...

i was returning an undefined value because i wasnt returning inside this function

client.detectLabels(params, function(err, response) {
        if (err) {
            rej(err)
            // console.log(err, err.stack); // an error occurred
        } else {
            console.log(`Detected labels for: ${photo}`)
            console.log("the data in side the file");
            console.log(response.Labels);
            resolve(response.Labels)
        } // if
    });

i needed to make promise with a resolve and rej and return it . dont forget to give resolve the response and rej the error like that

const imageReko =  (img) =>{
    return new Promise((resolve,rej)=>{
// somecode
   client.detectLabels(params, function(err, response) {
        if (err) {
            rej(err)
            // console.log(err, err.stack); // an error occurred
        } else {
            console.log(`Detected labels for: ${photo}`)
            console.log("the data in side the file");
            console.log(response.Labels);
            resolve(response.Labels)
        } 
    });
 )};
}