简体   繁体   English

为什么.split()函数不能与vue.js 2一起使用?

[英]Why won't .split() function work with vue.js 2?

I'm building my first app with vue.js, but am at a loss for why my go-to JavaScript functions aren't working. 我正在使用vue.js构建我的第一个应用程序,但是对于为什么我的JavaScript函数无法正常工作感到迷茫。

I'm importing a JSON file and trying to build a geojson object from it. 我正在导入JSON文件,并尝试从中构建geojson对象。 To do so, I need to split its latlong string (eg latlon:"52.3723138,0.4903593" ) into an array of numbers like [52.3, 0.49] . 为此,我需要将其latlong字符串(例如latlon:"52.3723138,0.4903593" )拆分为一个数字数组,例如[52.3, 0.49] latlon:"52.3723138,0.4903593" [52.3, 0.49] String.split() seems like the obvious function to use, but it's not working in this environment. String.split()似乎是显而易见的函数,但在这种环境下不起作用。

The json file is of this structure: json文件具有以下结构:

{ affiliates: [  ] }

I'm passing the JSON affiliates array to the makeGeoJson function as the data argument. 我将JSON关联数组传递给makeGeoJson函数作为数据参数。

Here's the code I'm trying: 这是我正在尝试的代码:

makeGeoJson(data) {
  var geoJsonFeatures = [];
  data.forEach(function(gym) {
    var feature = {
      "type": "Feature",
      "properties": gym,
      "geometry": {
        "type": "Point",
        "coordinates": [parseFloat(gym.latlon.split(",")[0]), parseFloat(gym.latlon.split(",")[1])]
      }
    };
    geoJsonFeatures.push(feature);
  });
},

The error I get says 我说的错误

TypeError: Cannot read property 'split' of undefined
    at eval (eval at 50 (http://localhost:8080/0.9506ad4988fb1af5b77e.hot-update.js:7:1), <anonymous>:47:37)

I'm using Vue.js 2.2 and used the vue-cli to set it up. 我正在使用Vue.js 2.2,并使用vue-cli对其进行了设置。

Is this related to why I am also not able to push data to arrays in this same function? 这是否与为什么我同样无法将数据推入数组的原因有关?

It is likely that your data is not as you expect. 您的数据可能不符合您的预期。 I would put a console.log() check: 我会把console.log()检查一下:

var geoJsonFeatures = [];
data.forEach(function(gym) {
    console.log(gym);
    var feature = {

Also, when you encounter an error like this, your script will not continue, so your push logic will not be reached. 另外,当您遇到这样的错误时,您的脚本将不会继续,因此将无法达到推送逻辑。

TypeError: Cannot read property 'split' of undefined means that the property gym.latlon does not exist. TypeError: Cannot read property 'split' of undefined意味着属性gym.latlon不存在。 It's not that split isn't working as expected or that split doesn't exist. 不是说拆分未按预期工作或拆分不存在。 That is exactly the reason you're not able to push the data. 这正是您无法推送数据的原因。 As soon as the error is thrown, it's going to propagate up the stack until it gets caught somewhere, so it's short circuiting your entire function. 一旦抛出错误,错误就会在堆栈中传播,直到被某个地方捕获为止,因此这会使整个函数短路。

// Error gets propagated to the caller of this function and beyond
// if you never handle it.
makeGeoJson(data) {
  var geoJsonFeatures = [];
  data.forEach(function(gym) {
    var feature = {
      "type": "Feature",
      "properties": gym,
      "geometry": {
        "type": "Point",
        // Throws here, so you get here once and never get past it.
        // gym.latlon does not exist. Double check property name and
        // object structure.
        "coordinates": [parseFloat(gym.latlon.split(",")[0]), parseFloat(gym.latlon.split(",")[1])]
      }
    };
    // You never get here.
    geoJsonFeatures.push(feature);
  });
},

As other people have pointed out, log gym and you will find that out is has no property latlon . 正如其他人指出的那样,登录gym ,您会发现latlon没有财产。

Also, another nice syntactic thing you can do to avoid splitting the string twice (once you have the object as you expect): 此外,还可以执行另一种不错的语法操作,避免将字符串分割两次(一旦您有了期望的对象):

gym.latlon.split(",").map(function (point) {
  return parseFloat(point);
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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