我有一个写入日志文件的模块。(对不起,但您明白了!)
require = patchRequire(global.require)
fs = require('fs')
exports.h =
log: ()->
for s in arguments
fs.appendFile "log.txt", "#{s}\n", (e)->
if (e) then throw e
Run Code Online (Sandbox Code Playgroud)
当我直接调用它时,它可以工作。但是,当我从回调中调用它时,例如casperjs start事件:
h = require('./h').h
casper = require('casper').create()
casper.start "http://google.com", ()->
h.log("hi")
casper.run()
Run Code Online (Sandbox Code Playgroud)
...我总是得到这个或类似的“未定义的” TyepError:
TypeError: 'undefined' is not a function (evaluating 'fs.appendFile("log.txt", "" + s + "\n", function(e) {
if (e) {
throw e;
}
})')
Run Code Online (Sandbox Code Playgroud)
谷歌搜索并没有很多线索!
CasperJS 在 PhantomJS(或 SlimerJS)上运行并使用其模块。它与nodejs不同。PhantomJS 的fs 模块没有appendFile函数。
当然,fs.write(filepath, content, 'a');如果在 casper 中使用,您可以使用附加到文件。如果您仍然想在 casper 和 node 中使用您的模块,那么您需要编写一些粘合代码,例如
function append(file, content, callback) {
if (fs.appendFile) {
fs.appendFile(file, content, callback);
} else {
fs.write(file, content, 'a');
callback();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1484 次 |
| 最近记录: |