【问题标题】:Somewhere [object Object] is printed在某处打印 [object Object]
【发布时间】:2015-03-02 00:59:39
【问题描述】:

我有几个 gulp 任务正在运行,完成后,就在 gulp 即将退出时,[object Object] 被打印到控制台。

不知道这是从哪里来的。不知道这是 gulp 问题还是由模块引起的。

覆盖console.log 没有帮助,因为[object Object] 已经打印为字符串。

这是 Coffee 中的 gulp 任务:

# Keep a reference so that we can kill it on process.exit
leinProcess = null

# Workaround to start the Clojure frontend server.
# This is needed when running E2E tests.
gulp.task('lein-run-static', (cb) ->

  leinProcess = spawn('lein', ['run-static'])

  # Here we compare each output until the line telling us that the frontend server is
  # up running, then we know it's ready.
  leinProcess.stdout.on('data', (data) ->

    line = data.toString().trim()

    # Do not print some duplicate lines
    if !line.match(/Requiring external|Running for version|Using gulpfile/)
      # Do not use gutil.log otherwise we see double dates
      console.log(line)

    if line.match(/Started SelectChannelConnector/)
      gutil.log('Frontend server started.')
      cb()
  )

  if config.debug
    leinProcess.stderr.pipe(process.stderr)

  # Make sure null is returned otherwise gulp will interpret leinProcess as
  # a gulp task stream
  return
)

我想在生成时使用stdio inherit,但之后我将失去解析标准输出的能力。

你有什么建议?

【问题讨论】:

  • 这就是coffeescript如此出色的原因,它让调试变得非常容易
  • 我疯狂地猜测data 是一个对象,在它上面调用toString 会给你[object Object]
  • 顺便问一下问题是什么?消息来自哪里或如何设置 stdio? :)
  • 是和不是。我想通了。数据已经在代码的其他地方转换为字符串。因此,在字符串“[object Object]”上调用toString() 将再次返回[object Object]。很简单。
  • @Heikki stdio 在类似的 GitHub 问题上被提及,这让我在这里问了同样的问题。

标签: node.js stream gulp


【解决方案1】:

继承除标准输出之外的所有内容:

leinProcess = spawn('lein', ['run-static'], { stdio: [0, 'pipe', 2]})

http://nodejs.org/api/child_process.html#child_process_options_stdio

作为简写,stdio 参数也可以是以下字符串之一:

'pipe' - ['pipe', 'pipe', 'pipe'],这是默认值

'ignore' - ['ignore', 'ignore', 'ignore']

'inherit' - [process.stdin, process.stdout, process.stderr] 或 [0,1,2]

【讨论】:

  • 是的,这有帮助。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-28
  • 1970-01-01
  • 2020-08-07
  • 2015-09-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多