创建自定义组件并使用NoFlo运行

时间:2015-09-23 15:50:09

标签: mongodb coffeescript noflo

我正在创建一个与MongoDB接口的自定义组件。我编写了一个CoffeeScript文件,它只连接到MongoDB,并将其存储在noflo / components文件夹中。

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

我将以下条目添加到component.json

"MongoBase": "components/MongoBase.coffee"

除此之外,我还创建了一个mongo.fbp文件来检查组件的流程。 FBP文件包含以下代码:

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

在运行noflo mongo.fbp时,出现以下错误:

/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)

我的代码或我用来运行代码的步骤是否有问题?

1 个答案:

答案 0 :(得分:0)

您可能已经想到了这一点,因为您问过几个月后,但我相信您需要在导出之前将getComponent()方法添加到您的课程中。

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

此外,在组件加载器工作的图表中,您需要指定组件所在的包。如果package.json / component.jsonname条目,请{{1}那么你必须在FBP图中指定它,如下所示:

"name": "mongo-base"

N.B。:加载程序会破坏程序包名称中'mongodb://localhost:27017/test' -> url DocReader(mongo-base/MongoBase) 的任何实例,因此需要将其考虑在内。例如。名称'noflo-'将变为'noflo-mongo',因此在调用软件包的组件时,您需要写入fbp 'mongo'而不是DocReader(mongo/MongoBase)