2015-09-28 27 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') 
     } 
    }); 
    }); 
});