在某些(不是所有)浏览器中读取 D3 javascript 中的 JSON 文件时出现问题

Problems reading JSON file in D3 javascript in some (not all) browsers

我在读取 JSON 文件后立即在 Chrome 中收到此错误。它在 Safari 和 Firefox 中正常工作。

console.log ("event -> "+ postcards.nodes[0].node.event); // "tally" in Safari "TypeError: Cannot read property 'node' of undefined" in Chrome.<

嵌套 JSON 文件中的级别是否有限制?我从 Drupal 视图生成 JSON。这是开始:

{"nodes":[{"node":{"w1":"","w2":"1","w3":"","w4":"", ...<

这里是 Javascript:

d3.json(
  "/sites/default/d3_files/json/toronto-wards.json", 
  function(error,wards) {
    d3.json("/postcards-json", function(error, postcards) {
      console.log ("event -> "+ postcards.nodes[0].node.event); // tally in Safari

我正在使用 macOS,我的朋友使用 Windows 在 Firefox 中遇到同样的错误。

根据要求,我认为这是 XHR 消息:

d3.min.js:1 XHR finished loading: GET "http://www.stopplastics.ca/postcards-json".

以下对你有用吗?

Promise.all(
  d3.json("/sites/default/d3_files/json/toronto-wards.json"),
  d3.json("/postcards-json")
)
.then(
  function([wards,postcards]) {
    console.log ("event -> "+ postcards.nodes[0].node.event); // tally in Safari

某些浏览器可能不支持解构数组,因此最好尝试以下旧浏览器(非 Chrome 和 Firefox):

Promise.all(
  d3.json("/sites/default/d3_files/json/toronto-wards.json"),
  d3.json("/postcards-json")
)
.then(
  function(results) {
    var wards=results[0],postcards=results[1];
    console.log ("event -> "+ postcards.nodes[0].node.event);
  }
);

根据评论,您提供 JSON 的 drupal 端点需要身份验证,因此在您未登录时为您提供空数据。

错误可能与使用的浏览器无关,但取决于您是否登录。

问题是由于未正确创建 Drupal 视图引起的。与 Chrome 浏览器无关。