我有一个写入日志文件的模块。(对不起,但您明白了!)
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
当我直接调用它时,它可以工作。但是,当我从回调中调用它时,例如casperjs start事件:
h = require('./h').h
casper = require('casper').create()
casper.start "http://google.com", ()->
h.log("hi")
casper.run()
...我总是得到这个或类似的“未定义的” TyepError:
TypeError: 'undefined' is not a function (evaluating 'fs.appendFile("log.txt", "" + s + "\n", function(e) {
if (e) {
throw e;
}
})')
谷歌搜索并没有很多线索!
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();
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句