0

I am creating a custom component which interfaces with MongoDB. I wrote a CoffeeScript file which just connects to MongoDB, and stored it at noflo/components folder.

MongoBase.coffee

noflo = require "noflo"
mongodb = require "mongodb"
url = require "url"

class exports.MongoBase extends noflo.Component
  constructor: ->
    super
    @inPorts =
      url: new noflo.Port()

    @inPorts.url.on "data", (data) =>
      try
        @parseConnectionString(data)
        @MongoClient = mongodb.MongoClient;
        @MongoClient.connect @serverUrl, (err, db) ->
          if err
            console.log("Error in connecting to MongoDB")
          else
            console.log("Connected to MongoDB")
      catch error
        console.log(error)
  parseConnectionString: (connectionString) =>
    databaseUrl = try
      url.parse(connectionString)
    catch error
      console.log(error) 
    [..., @serverUrl, @databaseName] = databaseUrl.split('/')
    @serverUrl = "mongo://" + @serverUrl

I added the following entry to component.json

"MongoBase": "components/MongoBase.coffee"

In addition to this, I created a mongo.fbp file to check the flow of the component. The FBP file has the following code:

'mongodb://localhost:27017/test' -> url DocReader(MongoBase)

On running noflo mongo.fbp, I get the following error:

/home/saurabh/workspace/noflo/node_modules/fbp/lib/fbp.js:1628
        edges.forEach(function (o, i) {
              ^
TypeError: Object #<Object> has no method 'forEach'
  at Object.parser.registerEdges (/home/saurabh/workspace/noflo/node_modules/fbp/lib/fbp.js:1628:15)
  at peg$c25 (/home/saurabh/workspace/noflo/node_modules/fbp/lib/fbp.js:60:50)
  at peg$parseline (/home/saurabh/workspace/noflo/node_modules/fbp/lib/fbp.js:749:30)
  at peg$parsestart (/home/saurabh/workspace/noflo/node_modules/fbp/lib/fbp.js:282:12)
  at Object.parse (/home/saurabh/workspace/noflo/node_modules/fbp/lib/fbp.js:1650:18)
  at Object.exports.loadFBP (/home/saurabh/workspace/noflo/lib/Graph.js:1065:33)
  at /home/saurabh/workspace/noflo/lib/Graph.js:1116:24
  at fs.js:268:14
  at Object.oncomplete (fs.js:107:15)

Is there something wrong with my code, or the steps I am using to run the code?

Saurabh Sood
  • 213
  • 1
  • 2
  • 9

1 Answers1

0

You may have figured this out already, as it's been several month's since you asked, but I believe you need to add the getComponent() method to your class before you export it.

noflo = require "noflo"
mongodb = require "mongodb"
url = require "url"

class MongoBase extends noflo.Component
  constructor: ->
    super
    @inPorts =
      url: new noflo.Port()

    @inPorts.url.on "data", (data) =>
      try
        @parseConnectionString(data)
        @MongoClient = mongodb.MongoClient;
        @MongoClient.connect @serverUrl, (err, db) ->
          if err
            console.log("Error in connecting to MongoDB")
          else
            console.log("Connected to MongoDB")
      catch error
        console.log(error)
  parseConnectionString: (connectionString) =>
    databaseUrl = try
      url.parse(connectionString)
    catch error
      console.log(error) 
    [..., @serverUrl, @databaseName] = databaseUrl.split('/')
    @serverUrl = "mongo://" + @serverUrl

MongoBase.getComponent = -> new MongoBase
exports.MongoBase = MongoBase

Additionally, in your graph for the component loader to work you need to specify the package your component lives in. If your package.json/component.json have a name entry like "name": "mongo-base" then you'd have to specify this in the FBP graph, like so:

'mongodb://localhost:27017/test' -> url DocReader(mongo-base/MongoBase)

N.B.: The loader clobbers any instances of 'noflo-' in the package name, so this needs to be taken into account. E.g. the name 'noflo-mongo' would get turned into just 'mongo', so when invoking the package's components you'd write in the fbp DocReader(mongo/MongoBase) and not DocReader(noflo-mongo/MongoBase).

Joshua Skrzypek
  • 448
  • 5
  • 9