Pinterest API:无法访问 'Metadata' 对象 属性,即使可以访问其他属性

Pinterest API: Cannot access 'Metadata' object property, even though other properties are accessible

我正在使用 Pinterest API。除了元数据对象 属性,我可以访问从我的响应返回的所有数据。 这是我获取 Pins 数据的代码:

let boarddata = [];
axios
   .get(`PINTEREST_API_URL`)
   .then(
     function(response) {
       boarddata = response.data.data;
       console.log(response.data.data);
     }.bind(this)
   );

Console.log 共 response.data.data[0]:

0:
  id: "167899892346462055"
link: "https://www.pinterest.com/r/pin/167899892346462055/5073939286663940267/f1b6e174ab1a06c38171839b6712e3a30ca6f73f0ee7ca480a72f8fb416f6537"
note: " "
url: "https://www.pinterest.com/pin/167899892346462055/"
attribution: null
image:
  original: {
    url: "https://i.pinimg.com/originals/27/e4/d6/27e4d67ba04f011da678fdd0da8c1d1a.jpg",
    width: 713,
    height: 1024
  }
__proto__: Object
metadata:
  link: {
    title: "Berry French Toast Casserole (Make Ahead Overnight) - Joyous Apron",
    description: "This Berry French Toast Casserole recipe is an eas…topped with berries. Make ahead the night before!",
    favicon: "https://i.pinimg.com/favicons/5c0314c869b732323c11…64e959475480.ico?70361f3f8b1918702ceaa18bc4b74ebe",
    locale: "en",
    site_name: "Joyous Apron"
  }
article: {
  name: "Berry French Toast Casserole",
  description: "This Berry French Toast Casserole recipe is an eas…topped with berries. Make ahead the night before!",
  authors: Array(0),
  published_at: "2018-07-26T00:00:00"
}
recipe: {
  name: "Berry French Toast Casserole",
  servings: {…},
  ingredients: Array(6)
}
__proto__: Object
original_link: "http://www.joyousapron.com/berry-french-toast-casserole/#wprm-recipe-container-2578"
__proto__: Object

我想映射响应对象并提取数据,以便在页面上显示用户图钉。我可以访问除元数据对象 属性 之外的所有其他内容,这是我最需要的。这是我想如何访问和使用对象数据的代码。

  showPins = boarddata => {
    let boardPins = this.state.boardPins;
    console.log(boarddata);
    boarddata.map((pin, index) => {
      const newPin = {
        id: pin.id,
        image: pin.image.original.url,
        name: pin.metadata.link.title,
        description:
          pin.metadata.article.description ||
          pin.metadata.link.description,
        ogLink: pin.original_link
      };
      boardPins.push(newPin);
    });
    this.setState({
      boardPins
    });
    console.log(this.state.boardPins);
  };

这个returns:

index.js:337 Uncaught (in promise) TypeError: Cannot read property 'title' of undefined
    at index.js:337
    at Array.map (<anonymous>)
    at t.a.showPins (index.js:333)
    at t.<anonymous> (index.js:316)

当我在执行地图功能之前控制台记录我需要的属性时,它们会显示出来。 (见下文)

console.log(boarddata[0].image.original.url); //https://i.pinimg.com/originals/27/e4/d6/27e4d67ba04f011da678fdd0da8c1d1a.jpg
console.log(boarddata[0].id); //167899892346517912
console.log(boarddata[0].metadata.link.title); //Berry French Toast Casserole (Make Ahead Overnight) - Joyous Apron
console.log(boarddata[0].metadata.link.description); //This Berry French Toast Casserole recipe is an eas…topped with berries. Make ahead the night before!
console.log(boarddata[0].original_link); //http://www.joyousapron.com/berry-french-toast-casserole/#wprm-recipe-container-2578

如有任何建议,我们将不胜感激

实际上不是 metadata 属性 未定义,而是 link 属性 of metadata 属性。您确定所有数据对象都相同吗?它们很可能不一致。似乎您只检查了响应数据的第一个元素(response.data.data[0]boarddata[0]

要进一步检查,请尝试以下操作:

showPins = boarddata => {
    let boardPins = this.state.boardPins;
    console.log(boarddata);
    boarddata.map((pin, index) => {
      console.log(`${index}: ${JSON.stringify(pin.metadata)}`);
      // ...
    });
    this.setState({
      boardPins
    });
    console.log(this.state.boardPins);
  };