【问题标题】:How to read a simple json file in keen js如何在敏锐的js中读取一个简单的json文件
【发布时间】:2015-09-28 13:11:15
【问题描述】:

我有几个基本的本地 json 文件。我想使用敏锐的 io 可视化 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" }

]

【问题讨论】:

    标签: javascript json keen-io


    【解决方案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')
          }
        });
      });
    });
    

    【讨论】:

      【解决方案2】:

      你有一些语法错误,试试这个:

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

      【讨论】:

      • 它没有解决问题,它给了我敏锐的错误自我:addEvents 不是一个函数
      • 这意味着您的客户端创建中有问题。您是否传递了正确的 projectID 和 writekey?
      • 是的,按键很好,但不知何故,该功能没有运行。例如,当我 console.log(storyData) 控制台中没有任何内容时
      • 您确定通过 data/web_stories.json 访问了您的 json 文件吗?好像您正在读取一个空(或不存在的)文件..
      • 你说得对,读取数据有问题,但是文件位于给定目录中并且不是空的
      猜你喜欢
      • 2021-12-09
      • 2020-10-06
      • 1970-01-01
      • 1970-01-01
      • 2016-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多