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

标签 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 中正确记录数据对象。

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

最佳答案

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

ul
  each val in title
    li= val.name

关于javascript - 显示 [object Object] 而不是对象属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56402634/

相关文章:

javascript - jQuery 查找最高父 TD

javascript - Express router 将 style.css 作为 request.params 传递并且可能调用路由两次?

express - 为什么 express-ejs-layout 无法呈现 layout.ejs?

node.js - 如何在 Typescript 中正确扩展请求的 header

javascript - Ember 数据在模板中未定义,但可在控制台中访问

javascript - 导航至 Bootstrap 步骤向导中的任何步骤

javascript - 如何在不重新加载页面的情况下在表单中创建 html 元素?

php - 数组数组到对象数组 php

C++: friend 特定对象(嵌套类)

c# - 从 C# 中的动态对象中删除属性及其值