显示[对象对象]而不是对象属性

时间:2019-05-31 23:17:48

标签: javascript express object strapi

我正在使用Strapi创建Api端点并使用expressjs使用这些端点。都是出于教育目的。

一切正常,除了在浏览器中显示对象属性。端点为http://localhost:1337/restaurants 以json格式显示我的数据:

[{"_id":"5cf19dd4a774126e9ca7f4fd","name":"Strapi Restaurant 1","description":"Strapi restaurant is a cosy restaurant delivering one of the very fastest and nicest dining experiences in the world, combining nods to tradition with fierce modernity, warmth with daring.\n# Strapi restaurant #\n- \n- \n1. \n2. \n3. ","createdAt":"2019-05-31T21:34:12.426Z","updatedAt":"2019-05-31T21:34:12.432Z","__v":0,"id":"5cf19dd4a774126e9ca7f4fd","categories":[{"restaurants":["5cf19dd4a774126e9ca7f4fd"],"_id":"5cf19e00a774126e9ca7f4fe","category":"Italian","createdAt":"2019-05-31T21:34:56.318Z","updatedAt":"2019-05-31T21:35:32.058Z","__v":0,"id":"5cf19e00a774126e9ca7f4fe"}]},{"_id":"5cf1af8ea774126e9ca7f500","name":"Strapi restaurant 2","description":"This is a restaurant that will serve you empty plates with no food. It is for your own good. You will loose weight quickly. Enjoy","createdAt":"2019-05-31T22:49:50.548Z","updatedAt":"2019-05-31T22:49:50.579Z","__v":0,"id":"5cf1af8ea774126e9ca7f500","categories":[{"restaurants":["5cf1af8ea774126e9ca7f500"],"_id":"5cf19e08a774126e9ca7f4ff","category":"Contemporary","createdAt":"2019-05-31T21:35:04.815Z","updatedAt":"2019-05-31T22:49:50.570Z","__v":0,"id":"5cf19e08a774126e9ca7f4ff"}]}]

我的快速路线使用axios访问api,响应显示在cmd中正确记录的数据

现在我的路线代码是:

var express = require('express');
var router = express.Router();
var axios = require("axios");

/* GET home page. */
router.get('/', function(req, res, next) {
     axios.get('http://localhost:1337/restaurants')
       .then(response => ( res.render('index', { title: response.data}), console.log(response) ));
          });
    module.exports = router;

我的PUG文件只是尝试使用变量“ title”显示“ reponse.data”:

extends layout
block content

  //  response from the strapiapp api
  //  p  restaurant name is : #{title}
  p  Restaurant name is: #{title.name}

这是我的cmd的屏幕截图 enter image description here

这是典型的设置,我已经使用不同的API进行了很多次。如果使用#{title},则浏览器将呈现[object Object];如果使用#{title.name},则将不显示任何内容。但是数据已正确记录到我的cmd中。

如何在浏览器中显示对象? title.name和titel.description无法正常工作,但数据已记录在我的cmd Express中,正确记录了数据对象。

知道为什么它不显示对象属性吗?

1 个答案:

答案 0 :(得分:1)

因此,正如亚历山大所评论的,response.data是一个数组。因此,您可以循环该数组以显示所有数据。我以前没有用过哈巴狗,但我认为这应该可行。

ul
  each val in title
    li= val.name