Troubles with Heroku Deployment

Troubles with Heroku Deployment
0.0 0

#1

Hello,

I’m currently working on the image abstraction layer project, and while it works in local, I’m having a weird “undefined variable” error when I try and deploy to Heroku. I have my MongoDB setup and everything, and I’m using the Bing.images npm package I believe in using Bing search efficiently as possible. The problem is, my body variable(line 61 - 71), which is an argument from a callback function, seems to be undefined when on Heroku. I’ve pasted my code below, and my full github project can be found here.

//Requirements
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const cors = require('cors');
const mongoose = require('mongoose');
const path = require('path');
const Bing = require('node-bing-api')({accKey: 'ad66f89d3a0c4a3c9c673dcf91bbb60f'});
var jade = require('jade');
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.static(path.join(__dirname, 'public')));

//Importing Mongoose Schema
const searchTerm = require('./models/searchTerm');
mongoose.connect(process.env.MONGODB_URI || 'mongodb://njanne19:link@ds125262.mlab.com:25262/image-search');


app.use(bodyParser.json());
app.use(cors());

//Home instruction page
app.get('/', (req, res, next) => {
  res.render('index');
});

//GET Call for image search + optional query
app.get('/api/imagesearch/:searchVal*', (req, res, next) => {
  var {
    searchVal
  } = req.params;
  var {
    offset
  } = req.query;

  var data = new searchTerm({
    searchVal,
    searchDate: new Date()
  });

  //Saving data back to DB
  data.save((err) => {
    if (err) {
      res.send('Error Saving');
    }
  });

  var searchOffset;
  if (offset) {
    if (offset == 1) {
      offset = 0;
      searchOffset = 1;
    } else if (offset > 1) {
      searchOffset = offset + 1;
    }
  }

  Bing.images(searchVal, {
    top: (10 * searchOffset),
    skip: (10 * offset)
  }, function(error, rez, body) {
    let bingData = [];
    for (var i = 0; i < 10; i++) {
      bingData.push({
        url: body.value[i].webSearchUrl,
        snippet: body.value[i].name,
        thumbnail: body.value[i].thumbnailUrl,
        context: body.value[i].hostPageDisplayUrl
      });
    }
    res.json(bingData);
  });


});


app.get('/api/recent', (req, res, next) => {
  searchTerm.find({}, (err, data) => {

    let historyData = [];
    for (var i = 0; i < data.length; i++) {
      historyData.push({
        "keyword": data[i].searchVal,
        "time-stamp": data[i].searchDate
      });
    }
    res.json(historyData);
  });
});



app.listen(process.env.PORT || 3000, () => {
  console.log("You good B");
});

#2

One obvious thing to do would be to check for an error right after line 61.


#3

@sabrawer

I’ve already done that. If you look at line 61, you see that the body variable is part of a callback function. The variable in the callback function is defined in a local environment, but undefined in a Heroku environment. Here is where my problem lies.


#4

I’m sorry, but don’t see where you handle the error. Here is what I think should be done:

function(error, rez, body) {
    if(error){
         // ........ handle error ....
         // body will probably be undefined
         return;
    }

If when the callback executes, error is “truthy”, the way I understand it, that means that Bing.images(...) had a problem and body will either be undefined or null or some such thing. Is the error being handled somewhere else, under the covers?


#5

@sabrawer

Thanks! That actually did the trick.