showing [object Object] 显示而不是对象属性

showing [object Object] displayed instead of the object properties

我正在使用 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的截图

这是典型的设置,我已经用不同的 api 做了很多次。如果我使用#{title},我的浏览器会呈现[object Object],如果我使用#{title.name},它什么也不会显示。但数据已正确记录到我的 cmd。

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

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

正如亚历山大所说 response.data 是一个数组。所以你可以循环这个数组来显示所有数据。我以前没有用过哈巴狗,但我认为这应该有用。

ul
  each val in title
    li= val.name