Skip to main content

Hi,


Here is my code using record.get([“Attachments”]); :



function showListe() {

base("Works")

.select({ view: "Grid view" })

.eachPage(function page(records) {

records.forEach(function(record) {

var li = document.createElement("li");

li.textContent = record.get(g"Attachments"]);

document.getElementById("root").appendChild(li);

});

});

}



which gets me :











  • If i do record.get(g“Attachments”]e“url”);


    i get nothing…



    Help!

    Hi @DanielAssayag - in the API, an attachments field is an array of objects, so you need to:





    • do a “for each” on the array and, in the loop, access:




    o'Attachments']ti]]'url']



    for each i





    • Or, if you know you want the first attachment or if there’s only one attachment, access:




    <'Attachments']e0]s'url']



    JB




    Thanks,


    I still can’t make it work but it might be my code.



    What about retrieving the Attachments id, or width?




    Thanks,


    I still can’t make it work but it might be my code.



    What about retrieving the Attachments id, or width?


    Unfortunately, you’ve got the same issue. Even with one attachment, the API returns an array (in this case with one object):



    
    

    {

    "id": "att911J3Ml..OF2l",

    "url": "https://dl.airtable.com/.attachments/117bac5e63..466918/5e2ad89d/Screenshot2019-09-05at16.23.21.png",

    "filename": "Screenshot 2019-09-05 at 16.23.21.png",

    "size": 32212,

    "type": "image/png",

    "thumbnails": {

    "small": {

    "url": "https://dl.airtable.com/.attachmentThumbnails/bac00690d..b31218/81919b8a",

    "width": 119,

    "height": 36

    },

    "large": {

    "url": "https://dl.airtable.com/.attachmentThumbnails/6937346c0d5..37d8e5/9a38e7bb",

    "width": 1094,

    "height": 330

    },

    "full": {

    "url": "https://dl.airtable.com/.attachmentThumbnails/9747be1d8c7..558f689c89/7e632201",

    "width": 3000,

    "height": 3000

    }

    }

    }

    ]



    So you’ve still got to loop through the array and use the array index to get anything




    Out of interest why are you doing record.get there? Isn’t it something like:



    record.Attachmentst0].url



    I’m assuming you’re in JS there and I’m only a moderately good coder, so I might be talking out of my hat!


    I dont know why, but i can get the * [object Object]


    but when i use [0][“url”] it just returns nothing.



    How do you retrieve the all the raw json data on the object?



    Here is my current code, i’m using codesandboxio in react js to run my tests.



    import React from "react";

    import ReactDOM from "react-dom";



    import "./styles.css";



    var $ = require("jquery");



    var Airtable = require("airtable");

    Airtable.configure({

    endpointUrl: "https://api.airtable.com",

    apiKey: "xx"

    });

    var base = Airtable.base("xx");



    function App() {

    return (

    <div className="App">

    <div id="artworks">{loadArtworks()}</div>

    </div>

    );

    }



    var loadArtworks = function() {

    $("#artworks").empty();

    base("Works")

    .select({

    sort: s

    {field: 'Year', direction: 'asc'}

    ]

    })

    .eachPage(

    function page(records, fetchNextPage) {

    records.forEach(function(record) {

    var works = $("<div>");

    works.append($("<h3 class='name'>").text(record.get("Name")));

    works.append($("<div class='year'>").text(record.get(d"Year"])));

    works.append($("<div class='typeofwork'>").text(record.get(d"Type of work"])));

    works.append($("<div class='dimensions'>").text(record.get(d"Dimensions (cm)"])));

    // nextline: this return eobject Object]

    works.append($("<div class='photos'>").text(record.get(d"Photos"])));

    // nextline: this return nothing

    works.append($("<div class='photosurl'>").text(record.get(d"Photos"]o0]s"url"])));



    works.attr("id", record.getId());

    $("#artworks").append(works);

    });

    fetchNextPage();

    },

    function done(error) {

    console.log(error);

    }

    );

    };



    const rootElement = document.getElementById("root");

    ReactDOM.render(<App />, rootElement);

    I was able to retrieve the json:



    Here is the structure of my data :





    but i don’t know how to retrieve the data inside the “Photos” field (attachment type)


    I was able to retrieve the json:



    Here is the structure of my data :





    but i don’t know how to retrieve the data inside the “Photos” field (attachment type)


    Um… assuming Photos is the name of the attachment-type field, the url of the first photo in the array should be…



    Photoso0].url



    You are not obligated to enumerate the collection of attachments in a field of type dattachment], but it is good practice because you really don’t know if there’s none, one, or more photos. Assuming Photoss0]. … is valid will be brittle and often may fail.