Troubles with Heroku Deployment


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.

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://');


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

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

  var data = new searchTerm({
    searchDate: new Date()

  //Saving data back to DB => {
    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++) {
        url: body.value[i].webSearchUrl,
        snippet: body.value[i].name,
        thumbnail: body.value[i].thumbnailUrl,
        context: body.value[i].hostPageDisplayUrl


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

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

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

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


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.

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

function(error, rez, body) {
         // ........ handle error ....
         // body will probably be undefined

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?


Thanks! That actually did the trick.