Javascript JSON变量未检索更新的值
我试图发布一个从JSON文件检索到的引用。我的代码每20秒发布一条tweet(出于测试目的,它是20秒)。我可以使用Javascript JSON变量未检索更新的值,javascript,jquery,json,node.js,twitter,Javascript,Jquery,Json,Node.js,Twitter,我试图发布一个从JSON文件检索到的引用。我的代码每20秒发布一条tweet(出于测试目的,它是20秒)。我可以使用server(quoteIndex)函数找到我的报价并将其放入JSON文件中。服务器(quoteIndex)向我的output.json文件添加一个新的quote。我知道,output.json每次在偶数索引上找到引号时都会更新{“quote”:“}部分。但是,在我的tweetIt()函数中,当我分配var quoteFile=require(“./output.json”)时,我
server(quoteIndex)
函数找到我的报价并将其放入JSON文件中。服务器(quoteIndex)
向我的output.json
文件添加一个新的quote。我知道,output.json
每次在偶数索引上找到引号时都会更新{“quote”:“}
部分。但是,在我的tweetIt()
函数中,当我分配var quoteFile=require(“./output.json”)
时,我的quoteFile.quote值不会更新。这是一个问题,因为我的javascript机器人是一个推特机器人,可以推特引用。twitter不允许重复引用,这给了我一个“重复状态”错误
这是主代码
// I wanted to start at the 8th index and continue with every even index.
var quoteIndex = 8;
// post a tweet every 20 seconds
setInterval(tweetIt, 1000*20);
tweetIt()
服务器(quoteNumber)
基本上,当我使用node.js运行代码时,第一个引号将发出tweet,因为JSON文件中有一个引号。然后,当使用quoteIndex=quoteIndex+2
查找下一个报价时,我的JSON文件会按照预期在我的项目文件夹中更新。我的主要问题是,在我的tweetIt()
函数中,quoteFile.quote
没有显示更新的quote,即使JSON文件为我提供了更新的quote。如何获得更新的报价
任何提示都将不胜感激。只要您需要()
一个文件,无论是模块、JSON文件还是本机插件,一旦成功加载,它就会被缓存。如果您需要能够重新加载JSON文件,那么您应该每次手动调用readFile()
和JSON.parse()
生成的文件数据。我对如何实现readFile()
感到困惑。我可以通过执行fs.readFile('./output.json',…)
来进行操作,但其中的所有内容(引号数据)都无法分配给我的params
变量。当然可以,只需将readFile()
调用下面的所有逻辑放在回调中即可。如果要坚持同步调用,请使用readFileSync()
。
function tweetIt() {
//
// This will start looking for quotes to post
// It will put the quote in a JSON file
//
quoteIndex = quoteIndex + 2;
console.log('value of index: ' + quoteIndex)
server(quoteIndex);
var js = require('json-update');
js.load('./output.json', function(err, obj) {
console.log("Loaded from json:");
console.log(obj); // loads most recent quote
}); // this is what I want to tweet
var quoteFile = require('./output.json');
// Read from JSON file for the quote
var params = {
status: quoteFile.quote
}
//
// prints same quote each time, does not update
//
console.log("quote to tweet: " + quoteFile.quote)
// tweet a quote
//
// The first quote will tweet, however when the JSON file
// updates, I will get a "duplicate status" error.
// This is because my quoteFile still has not updated.
//
T.post('statuses/update', params, getData);
function getData(err, data, response) {
if (err) {
console.log("Something went wrong!: " + err);
} else {
console.log("Tweeted something!");
}
}
}
function server(quoteNumber) {
var fs = require('fs');
var request = require("request"),
cheerio = require("cheerio"),
url = "https://en.wikiquote.org/wiki/A_Song_of_Ice_and_Fire";
request(url, function (error, response, body) {
if (!error) {
var $ = cheerio.load(body);
var quote;
var json = {quote : ""};
$( "div.mw-parser-output ul" ).filter(function( INDEX ) {
// even indexed quotes are the ones I want
if (INDEX % 2 == 0 && (INDEX == quoteNumber)) {
quote = $( this ).text();
json.quote = quote; // this also has the most recent quote
}
});
} else {
console.log("We’ve encountered an error: " + error);
}
//
// Write our quotes out to a JSON file.
//
fs.writeFile('output.json', JSON.stringify(json, null, 4).replace(/\\n/g, " "), function(err){
console.log('File successfully written! - Check your project directory for the output.json file');
})
});