简体   繁体   中英

How to read a simple json file in keen js

I have a couple of basic local json files. I want to visualize the json data using keen.io. But somehow I cannot send my events to keen. There are no errors in the console.

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')
  }

The data looks like this

[
{ 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" }

]

I'm seeing data defined a few times. How about this:

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')
      }
    });
  });
});

you have some syntax errors, try this:

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')
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM