我编写了一个javascript程序,我希望它能够将api调用中的数据写入json文件。由于某种原因,编译器一直说fs.writeFile不是函数,尽管在。
中需要fs代码段:
var fs = require('fs')
function writeDataTofile(data){
fs.writeFile("../transactionData.json",data,"utf8",function(err){
if (err) return
})
console.log("file written")
}
数据来自这里:
function getApiData(tx){
console.log("api initiated")
//run chrome extension below to allow CORS:
//https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi/related?hl=en-US
var query = "https://blockchain.info/rawtx/"+tx+"/$tx_hash";
request.get(query,function(err,res){
var txValue = JSON.parse(res.text).out[0].value;
var script = JSON.parse(res.text).out[0].script;
var recipientAddr = JSON.parse(res.text).out[0].addr;
fileData = res.text
console.log(txValue, script)
if(txValue != undefined && script != undefined){
getValue(txValue)
//scriptToAddress(script) //bitcore not working yet
matchAddress(recipientAddr)
writeDataTofile(fileData)
return res.text
}
else{
areConditionsMeet = false
}
})
}