'items`聚合物的预期数组

时间:2015-11-11 19:45:56

标签: polymer-1.0

我正在学习Polymer 1.0,而我正试图从api中提取天气数据。一开始我硬编码了URL,我能够获取数据,因为我可以在开发控制台中看到它。现在我正在尝试<location path="some-path/some-directory" inheritInChildApplications="false"> <system.webServer> <httpModules> <add name="ImageResizingModule" type="ImageResizer.InterceptModule"/> </httpModules> </system.webServer> </location>,但我在控制台中遇到了一个奇怪的错误:

dom-repeat

我有我的index.html导入webcomponents和我正在使用的weather.html文件。我将要粘贴两个文件的代码。

的index.html:

[dom-repeat::dom-repeat]: expected array for 'items', found Object {city: Object, cod: "200", message: 0.0058, cnt: 3, list: Array[3]}  polymer.html:3923

天气forecast.html:

 <!DOCTYPE html>
 <html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes">

  <title>Weather</title>
  <script src="bower_components/webcomponentsjs/webcomponents.min.js">    </script>
  <link rel="import" href="bower_components/paper-header-panel/paper-header-panel.html">
  <link rel="import" href="bower_components/paper-toolbar/paper-toolbar.html">
  <link rel="import" href="bower_components/iron-icons/iron-icons.html">
  <link rel="import" href="bower_components/paper-icon-button/paper-icon-button.html">
  <link rel="import" href="bower_components/weather-forecast/weather-forecast.htm">
 <link rel="stylesheet" href="css/mystyle.css">
 </head>
   <body class="fullbleed layout vertical">
     <weather-forecast></weather-forecast>
   </body>
 </html>

这段代码有什么问题????

2 个答案:

答案 0 :(得分:1)

错误消息的第一部分应该让您入门:

  

[dom-repeat :: dom-repeat]:'items'的预期数组

这会引导你走向这一行,因为这是你的dom-repeat,需要一个'项目':

M_LINK.ISDIRECTED

您遇到的问题是<template is="dom-repeat" items="{{response}}">应该是一个数组,但事实并非如此。从错误消息的末尾看,响应实际上是一些JSON:

  

找到对象{city:Object,cod:“200”,消息:0.0058,cnt:3,list:Array [3]}

JSON似乎包含一个数组(列表),所以也许这就是你需要的?尝试更新到:

response

答案 1 :(得分:0)

使用Firebase和Polymer时遇到了这个问题。
在这种情况下,最好检查一下您想要移交给dom-repeat的数据。

幸运的是,它很容易转换(在我的情况下是一个对象到一个数组)..

Object.values()

在其他情况下,您可能想要使用..

Array.from()

您可以在此处详细了解这两种方法:
Object.values() {
{3}}

但是,重要的是要知道Internet Explorer中不支持这两种方式