0

Hi I want to create an object of ProfileComparator in another index.js file but I am getting an error.

strategy.js

var cosineUtils = require("./jscosine");

var ProfileComparator = function(algo, x, y, threshold) {
  this.algo = algo;
  this.x = x;
  this.y = y;
  this.threshold = threshold;
};

ProfileComparator.prototype.findMatches = function() {
  return this.algo(this.x, this.y, this.threshold);
};

var cosineAlgoStrategy = function(x, y, threshold) {
  var similarityCount = cosineUtils.cosineSimilarity(x, y);

  if (similarityCount >= threshold) {
    return y;
  }

  console.log("------------------------------------");
  console.log("cosine");
  console.log("------------------------------------");
};

var pearsonAlgoStrategy = function(x, y, threshold) {
  console.log("------------------------------------");
  console.log(threshold);
  console.log("------------------------------------");
};

I am able to create object of ProfileComparator in strategy.js not in other javascript file like below

var cosineAlgo = new ProfileComparator(cosineAlgoStrategy, "x", "y", 0.9);
return cosineAlgo.findMatches();

index.js

I am trying to do same in index.js but I am getting an error here:

var strategyUtils = require("./strategy");

function computeSimilarity(x, user) {
  var cosineAlgo = new ProfileComparator(cosineAlgoStrategy, x, y, 0.9);
  return cosineAlgo.findMatches();
}

StackTrace:

ReferenceError: ProfileComparator is not defined
    at computeSimilarity (/user_code/index.js:187:24)
    at /user_code/index.js:232:16
    at process._tickDomainCallback (internal/process/next_tick.js:135:7)

Does anyone know how to resolve it ?

3
  • I don't see any exports Commented Sep 3, 2017 at 18:01
  • You have not exported ProfileComparator from strategy.js file. Commented Sep 3, 2017 at 18:02
  • Use 'export' befor func name. Then it will accessible outside of local file Commented Sep 3, 2017 at 18:03

4 Answers 4

2

You need to export your ProfileComparator comparator function from strategy.js file like this

module.exports = ProfileComparator

And in your index.js, require it like this

var ProfileComparator = require("./strategy");
Sign up to request clarification or add additional context in comments.

1 Comment

I am getting this now ReferenceError: cosineAlgoStrategy is not defined at computeSimilarity (/user_code/index.js:187:42) at /user_code/index.js:232:16 at process._tickDomainCallback (internal/process/next_tick.js:135:7) here var cosineAlgo = new ProfileComparator(cosineAlgoStrategy, x, y, 0.9);
1

Here you go with your exported class and algorithms that you might wanna use in your Comparator:

const ProfileComparator = function(algo, x, y, threshold) {
  this.algo = algo
  this.x = x
  this.y = y
  this.threshold = threshold
}

ProfileComparator.prototype.findMatches = function() {
  return this.algo(this.x, this.y, this.threshold)
}

const cosineAlgoStrategy = function(x, y, threshold) {
  var similarityCount = cosineUtils.cosineSimilarity(x, y)

  if (similarityCount >= threshold) {
    return y
  }

  console.log('------------------------------------')
  console.log('cosine')
  console.log('------------------------------------')
}

const pearsonAlgoStrategy = function(x, y, threshold) {
  console.log('------------------------------------')
  console.log(threshold)
  console.log('------------------------------------')
}

module.exports = { ProfileComparator, cosineAlgoStrategy, pearsonAlgoStrategy }

This would be your new index.js file where you import all you needed modules:

const { ProfileComparator, cosineAlgoStrategy, pearsonAlgoStrategy } = require("./strategy");

function computeSimilarity(x, user) {
    var cosineAlgo = new ProfileComparator(cosineAlgoStrategy, x, y, 0.9);
    return cosineAlgo.findMatches();
}

7 Comments

I am getting TypeError: ProfileComparator is not a constructor at computeSimilarity (/user_code/index.js:191:20) at /user_code/index.js:236:16 at process._tickDomainCallback (internal/process/next_tick.js:135:7)
@Williams I corrected my code. I was just typing, what I had on my mind.
Thanks a lot :) it worked. I didn't know that we can export multiple too using this syntax
@Williams you are welcome. Sure you can use this 'trick', creating an export object, which you then destruct on the import side to access the functionalities more easily.
If I add var instead of const here const { ProfileComparator, cosineAlgoStrategy, pearsonAlgoStrategy } = require("./strategy"); then it won't work ?
|
1

Fix

You need to export the ProfileComparator!
Add: module.exports = ProfileComparator; to the end of the strategy.js file!
Now you can require it in other files with something like this:
var ProfileComparator = require('path/to/the/module');.
Dont forget to checkout this: https://nodejs.org/docs/latest/api/modules.html#modules_module_exports

Explanation of the error

The error occurred because you tried to create a new instance of the ProfileComparator without it beeing in scope!

3 Comments

I am getting this now ReferenceError: cosineAlgoStrategy is not defined at computeSimilarity (/user_code/index.js:187:42) at /user_code/index.js:232:16 at process._tickDomainCallback (internal/process/next_tick.js:135:7) here var cosineAlgo = new ProfileComparator(cosineAlgoStrategy, x, y, 0.9);
Right, this variable is not in scope!
I will add the solution to my answer later!
0

Change

var ProfileComparator = function(algo, x, y, threshold) {
  this.algo = algo;
  this.x = x;
  this.y = y;
  this.threshold = threshold;
};

To

export const ProfileComparator = function(algo, x, y, threshold) {
  this.algo = algo;
  this.x = x;
  this.y = y;
  this.threshold = threshold;
};

Import as

Import { ProfileComparator } from '<file location>'

1 Comment

not natively working in nodejs, since export/import isn't standardized, yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.