2015-09-28 85 views
0

我有几个基本的本地json文件。我想用keen.io来可视化json数据。但不知何故,我无法将我的活动发送给敏锐。控制台中没有错误。如何在敏锐的js中读取简单的json文件

var client = new Keen({ 
    projectId: "key", 
    writeKey: "key" 
    }); 



var data = $.getJSON("data/web_stories.json", function(data) { 
    var storyData = data 
    Keen.ready(function(){ 
      var multipleEvents = { 
       "stories": data 
}; 
// Send multiple events to several collections 
client.addEvents(multipleEvents, function(err, res){ 
    if (err) { 
    console.log('there is an error!') 
    } 
    else { 
    console.log('data sent') 
    } 

的数据看起来像这样

[ 
{ link: "www.link.com", 
    heading: 'here is the heading', 
    image: "www.image.com" }, 
{ link: "www.link.com", 
    heading: 'here is the heading', 
    image: "www.image.com" } 

] 

回答

0
你有一些语法错误

,试试这个:

var client = new Keen({ 
    projectId: "key", 
    writeKey: "key" 
    }); 

var multipleEvents; 

var data = $.getJSON("data/web_stories.json", function(data) { 
    var storyData = data 
    Keen.ready(function(){ 
      multipleEvents = { 
       "stories": data 
      }; 
    }); 
}; 
// Send multiple events to several collections 
client.addEvents(multipleEvents, function(err, res){ 
    if (err) { 
    console.log('there is an error!') 
    } 
    else { 
    console.log('data sent') 
    } 
} 
+0

它不解决这个问题,它给我的错误,从热衷于它的自我:是ADDEVENTS不是一个函数 – Imo

+0

意思有什么错误在你的客户创造。你是否传递了正确的projectID和writekey? – gbalduzzi

+0

是的键很好,但不知何故该功能没有运行。例如,当我console.log(storyData)控制台中没有任何东西 – Imo

1

我看到data定义了若干倍。这个怎么样:

var client = new Keen({ 
    projectId: "key", 
    writeKey: "key" 
}); 

Keen.ready(function(){ 
    $.getJSON("data/web_stories.json", function(data) { 
    // Send multiple events to several collections 
    client.addEvents({ 'stories': data }, function(err, res){ 
     if (err) { 
     console.log('there is an error!') 
     } 
     else { 
     console.log('data sent') 
     } 
    }); 
    }); 
});