【发布时间】:2020-01-11 17:33:58
【问题描述】:
我正在使用 Pinterest API。除了元数据对象属性外,我可以访问从我的响应返回的所有数据。 这是我获取 Pins 数据的代码:
let boarddata = [];
axios
.get(`PINTEREST_API_URL`)
.then(
function(response) {
boarddata = response.data.data;
console.log(response.data.data);
}.bind(this)
);
response.data.data[0]的Console.log:
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
我想映射响应对象并提取数据,这样我就可以在页面上构建用户 pin 的显示。除了我最需要的元数据对象属性之外,我可以访问其他所有内容。这是我想如何访问和使用对象数据的代码。
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);
};
这会返回:
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
任何建议将不胜感激
【问题讨论】:
标签: javascript reactjs metadata pinterest