Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 从节点API提取数据并保存输出_Node.js - Fatal编程技术网

Node.js 从节点API提取数据并保存输出

Node.js 从节点API提取数据并保存输出,node.js,Node.js,这是对上一个问题的延续,但是,我相信这个问题与上一个问题有很大的不同 我使用的是试图通过 根据自述文件,数据是以JSON格式检索的 我想得到一个活跃的重量级拳击手的列表,以及与所述拳击手相关的信息,并将输出保存为json,然后我将对其进行清理以供进一步分析 不知道我做错了什么,但是; i、 )我无法打印与上述boxers关联的json输出,如自述输出(对于getRatings方法)所示,并且无法将此数据转储到json或csv文件中。输出文件只是空的 这是我的密码: const boxrec =

这是对上一个问题的延续,但是,我相信这个问题与上一个问题有很大的不同

我使用的是试图通过

根据自述文件,数据是以JSON格式检索的

我想得到一个活跃的重量级拳击手的列表,以及与所述拳击手相关的信息,并将输出保存为json,然后我将对其进行清理以供进一步分析

不知道我做错了什么,但是; i、 )我无法打印与上述boxers关联的json输出,如自述输出(对于getRatings方法)所示,并且无法将此数据转储到json或csv文件中。输出文件只是空的

这是我的密码:

const boxrec = require("boxrec").Boxrec;
const fastcsv = require('fast-csv');
const fs = require('fs');
async function getCookieJar(){
    try {
        const cookieJar = await boxrec.login('**','****');
        return cookieJar;
    } catch (e) {
        console.log("Login error: " + e);
    }
};
async function writeData() {
    const cookieJar = await getCookieJar();
    var boxers = await boxrec.getRatings(cookieJar, {
        "division": "Heavyweight",
        "sex": "M",
        "status": "a"
    });
    const ws = fs.writeFileSync('C:\\Users\\User\\Documents\\testing.json', JSON.stringify(boxers));
};
try {
    writeData();
} catch (error) {
    console.log("Error in writeData: " + error);
}
如果我只是使用console.log(boxers)打印出boxers,这就是生成的输出:

i {
  '$': [Function: initialize] {
    fn: initialize { constructor: [Circular], _originalRoot: [Object] },
    load: [Function],
    html: [Function],
    xml: [Function],
    text: [Function],
    parseHTML: [Function],
    root: [Function],
    contains: [Function],
    merge: [Function],
    _root: {
      type: 'root',
      name: 'root',
      namespace: 'http://www.w3.org/1999/xhtml',
      attribs: [Object: null prototype] {},
      'x-attribsNamespace': [Object: null prototype] {},
      'x-attribsPrefix': [Object: null prototype] {},
      children: [Array],
      parent: null,
      prev: null,
      next: null
    },
    _options: {
      withDomLvl1: true,
      normalizeWhitespace: false,
      xml: false,
      decodeEntities: true
    }
  }
}

输出开始处的
i
确定该对象不是JavaScript普通对象。确定该类是类型为
i
(精简JavaScript)的类的实例,该类与
JSON.stringify()
不兼容

通过查看源代码和,您可以看到它实际返回的是一个
BoxrecPageRatings
实例,它不是一个普通的JavaScript对象,而是一个您应该通过调用方法、访问属性等来处理的实例

我想您应该阅读源代码来理解正在发生的事情,或者访问他们的源代码,并询问正在发生的事情,因为结果不是他们在自述文件中声明的JSON对象

顺便说一句,在JavaScript中序列化实例的常用方法是访问要保存的属性,因为对整个实例执行
JSON.stringify()
不是一件好事(如您所注意的):


您是否尝试过
console.log(JSON.parse(boxers))
?@Subburaj我没有,这将返回以下消息:UnhandledPromiseRejectionWarning:SyntaxError:JSON.parse()位置1处JSON中的意外标记o尝试
console.log(JSON.parse(JSON.stringify(boxers))
@Subburaj输出为空->{}表示您的
boxrec.getRatings
返回的结果为空,因此您的CSV也为空。检查它是否返回任何数据??我明白了!,有意义的是,当我使用getPersonById方法并访问我想要查看和保存的属性时,这是可行的,但getRatings方法不行,所以属性可能与getRatings不同。我看到有一个
boxers()
getter,它映射到
.boxers
属性。尝试
console.log(boxers.boxers)然后看看有什么。我没有看到任何关于这方面的文档,所以我想你能做的最好的事情就是阅读这些源代码并进行测试。还有一个
.output
属性可供检查。也许这就是您想要的JSON。boxers.output似乎可以生成所需的输出。非常感谢你!
var boxers = await boxrec.getRatings(cookieJar, {
    "division": "Heavyweight",
    "sex": "M",
    "status": "a"
});

// I don't know what properties it has actually, but you get the point
let saveString = JSON.stringify({
    name: boxers.name,
    id: boxers.boxerId,
    weird: boxers.thing,
    list: boxers.getList().map(yay => yay.name),
    // [...] etc
});