Polymer Iron-Ajax Get Method从返回res.json的expressJS路由中检索结果

时间:2017-06-09 14:06:01

标签: json heroku polymer iron-ajax

我的api终点在Heroku免费帐户(expressJS)网址IS HERE上运行

它显示来自expressJS路线的json响应。数据看起来像这样

[{"_id":"5938b81bd263a5144c9d7d17","title":"mlab first entry","__v":0},
 {"_id":"5939efc7fac2b71aac4add11","title":"Second Entry","__v":0},
 {"_id":"5939efd0fac2b71aac4add12","title":"Second Entry","__v":0}]

我从localhost(http://127.0.0.1:8081/view1)运行的聚合物代码:`

<!--
@license
Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->

<link rel="import" href="../bower_components/polymer/polymer-element.html">
<link rel="import" href="shared-styles.html">
<link rel="import" href="../bower_components/polymer/polymer.html">
<link rel="import" href="../bower_components/iron-ajax/iron-ajax.html">


<dom-module id="my-view1">
  <template>

   <iron-ajax
      auto 
      url="https://shielded-caverns-24265.herokuapp.com/"
      handle-as="json"
      last-response="{{response}}">
   </iron-ajax>
 <<!--tried items={{response}} -->
 <template is="dom-repeat" items="[[response.title]]">
<div>
  <a href="{{item.title}}">   &nbsp &nbsp {{ item.title}}</a>
</div>
</template>


 <!-- this code works and pull the data from https://reqres.in/api/users?page=2
<template is="dom-repeat" items="[[response.data]]">
<div>
  <a href="{{item.first_name}} {{item.last_name}}"> {{item.first_name}}  &nbsp &nbsp {{ item.last_name}}</a>
</div>
</template>
-->


  </template>


  <script>
    class MyView1 extends Polymer.Element {
      static get is() { return 'my-view1'; }
    }

    window.customElements.define(MyView1.is, MyView1);
  </script>
</dom-module>

`

但是当我访问我的聚合物页面时,它没有显示任何数据......我做错了什么?

这是我的路线代码:

     router.get('/', function(req, res){
     Comment.find(function(err, comments){
        res.json(comments);
});
});

我是否需要格式化服务器res.json?或者使用res.format(对象)?  我感谢任何帮助。

0 个答案:

没有答案