Search code examples
javascriptnode.jsapiexpressunirest

NodeJS same results for two different requests


SOLVED THANKS TO @Patrick Evans

I am creating my own web project and i need some help. At the website, the client is requested to upload a face photo. Then , when the client presses "upload" button , his photo is sent with a request to "face++" api which gives back details about the photo such as emotions and gender, at a different ejs page. At the new page the client sees his photo and below are the details about his photo.

It works fine , but when the client gets back to the homepage, and chooses a different new photo, then presses upload , he sees his new photo that he chose, but gets the same details as were at the last photo (details from face++ api).

I use the following:

  • express.
  • unirest for making the request to "face++" api.
  • cloudinary for having a url, and using the url at the face++ request(the url represents the client's photo).
  • multer for storing at local drive.

When i print out the details which return from "face++" api , at the "requestFromApi.end()" function , i already notice the details havent changed from prior request, but i do notice at cloudinary that a different photo was uploaded.

I attached my app.js code.

Thanks alot for any help :)

    var unirest = require("unirest");
    var requestFromApi = unirest("POST", "https://faceplusplus- 
    faceplusplus.p.rapidapi.com/facepp/v3/detect");
    var cloudinary = require("cloudinary").v2;
    const express = require('express');
    const multer = require('multer');
    const app = express();
    const path = require("path");
    var bodyParser = require("body-parser")
    app.use(bodyParser.json({ limit: '50mb' }));
    app.use(bodyParser.urlencoded({     // to support URL-encoded bodies
     limit: '50mb',
     extended: true
                    }));

    app.set("view engine", "ejs");

    cloudinary.config({
    cloud_name: 'dkqvnprcj',
    api_key: '756886954695832',
    api_secret: 'you know i cant give you that...'
     });

   app.get("/", function (req, res) {
   res.render("FaceApp.ejs");
   });

   // SET STORAGE
   var storage = multer.diskStorage({
   destination: function (req, file, cb) {
   cb(null, 'uploads')
   },
   filename: function (req, file, cb) {
   cb(null, file.fieldname + '-' + Date.now() + 
   path.extname(file.originalname));
   }
   })

   var upload = multer({ storage: storage })

   app.post('/upload', upload.single('photo'), (req, res) => {

   if (req.file) {

cloudinary.uploader.upload(req.file.path, function (error, result) {
  //console.log(req.file);
  let result_ = result;
  let url = result.url;

  //console.log(url)

  requestFromApi.query({
    return_attributes: "gender,age,smiling,facequality,eyestatus,emotion,ethnicity,beauty,skinstatus",
    image_url: url
  });

  requestFromApi.headers({
    "x-rapidapi-host": "faceplusplus-faceplusplus.p.rapidapi.com",
    "x-rapidapi-key": "9dd7fa4266mshf1c29ba307ecf2dp1bb1dajsna431d00b6273",
    "content-type": "application/x-www-form-urlencoded"
  });

  requestFromApi.form({});

  requestFromApi.end(function (result) {
    if (result.error) throw new Error(result.error);
    else {

      let detailsFromApi = JSON.parse(JSON.stringify(result.body.faces));

      detailsFromApi.forEach(function (element) {
        console.log(element);
      });

       res.render("image",{result_ : result_, detailsFromApi:detailsFromApi});
    }

  });

});
       }
     else throw 'error';
     });

Solution

  • SOLVED THANKS TO @Patrick Evans [1]: https://stackoverflow.com/users/560593/patrick-evans

    I had to make sure i call "Unirest" at every single Post request, and not just at the beginning of the execution.