user3538384 user3538384 - 1 month ago 15
Node.js Question

NodeJS- ReferenceError: wagner is not defined

I don't have experience at all with nodejs and I am learning. I have a code, which I am testing and giving me the following errors, and I don't understand why:

1) Part 3 Assessment Tests "before all" hook:
ReferenceError: wagner is not defined
at Object. (fx.js:2:31)
at Object. (dependencies.js:3:10)
at Context. (test.js:29:20)

2) Part 3 Assessment Tests "after all" hook:
TypeError: Cannot read property 'close' of undefined
at Context. (test.js:65:11)

These are the programs involved in the issue:

test.js --------------

var assert = require('assert');
var express = require('express');
var fs = require('fs');
var status = require('http-status');
var superagent = require('superagent');
var wagner = require('wagner-core');

var URL_ROOT = 'http://localhost:3000';
var PRODUCT_ID = '000000000000000000000001';

describe('Part 3 Assessment Tests', function() {
var server;
var app;
var succeeded = 0;
var finalCharge;

var Category;
var Config;
var fx;
var Product;
var Stripe;
var User;

before(function() {
app = express();

// Bootstrap server
models = require('./models')(wagner);
dependencies = require('./dependencies')(wagner);

// Make models available in tests
var deps = wagner.invoke(function(Category, fx, Product, Stripe, User, Config) {
return {
Category: Category,
fx: fx,
Product: Product,
Stripe: Stripe,
User: User,
Config: Config
};
});
...


dependencies.js -------------

var wagner = require('wagner-core');
var fs = require('fs');
var fx = require('./fx')(wagner);
var Stripe = require('stripe');

module.exports = function(wagner) {
var stripe =

// TODO: Make Stripe depend on the Config service and use its `stripeKey`
// property to get the Stripe API key.
wagner.factory('Stripe', function() {
return Stripe(Config.stripeKey);
});

wagner.factory('fx', fx);

wagner.factory('Config', function() {
return JSON.parse(fs.readFileSync('./config.json').toString());
});

var Config = wagner.invoke(function(Config) {
return Config;
});
};


fx.js --------------------------

var superagent = require('superagent');
var _ = require('underscore')(wagner);

module.exports = function(Config) {
...
};


I think I shouldn't have any problem because wagner is defined on test.js and passed as parameter to dependencies.js, and it is passing it on to fx.js.

1- Could you tell me what is wrong in the code ?
2- The second error, I have not cue why is it happening.

Please, help

Answer

You're trying to use wagner in fx.js before you execute the exported function and outside of the scope that wagner is passed to. Also, Config seems a bit misleading if you're passing wagner to that function instead.

If you can defer loading underscore, you could do something like:

var superagent = require('superagent');
var _;

module.exports = function(Config) {
  if (!_)
    _ = require('underscore')(Config);
  // ...
};