javascriptnode.jsapimongoose-schemascopus

Populate Mongo database with mongoose and API


So for a personal project, I am trying to make a database of conferences and their papers. to do this I am using Mongo DB community server, node.js, mongoose and the Elsevier Scopus API. So far I have made my own JSON file with all the relevant information for each conference but once I am trying to populate each conference with relevant papers of each year's edition I am unable to properly retrieve the relevant data of the JSON file retrieved from Scopus.

The schema for my db is the following :

var edition_papers = new mongoose.Schema({
    title: String,
    author: String,
    abstract: String
});

const papers = mongoose.model('papers', edition_papers);

// will each hold a reference to a multiple document from the papers collection
var conference_edition = new mongoose.Schema({
    edition: Number,
    papers: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'papers'
    }
});

const edition = mongoose.model('editions', conference_edition);


const ConferenceSchema = new Schema({
    acronym : String,
    name : String,
    area : String,
    subarea : String, 
    location : String,  
    year: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'editions'
    }
}); 

For my populate function:

function apiCall(conf_name, year) {
    var url = 'https://api.elsevier.com/content/search/scopus?query=CONFNAME(' + conf_name + ')%20AND%20DOCTYPE(cp)%20AND%20SRCTYPE(p)%20AND%20PUBYEAR%20%3D%20' + year + '&apiKey=' + key;
    fetch(url).then(response => response.json()).then(data => {
    console.log(data)
    })
}

function getPapers(year, conf_name) {
    //var json_papers = apiCall(conf_name, year);
    var temp = [];
    for (let i = 0; i < apiCall(conf_name, year).search-results.totalResults; i++) {
        temp[i] = new Papers({
            title: json_papers.entry[i].title,
            author: json_papers.entry[i].creator,
        })
    }

    return temp;
}

function getEdition(conf_name) {
    var years = [2020, 2019, 2018, 2017, 2016, 2015];
    var temp = [];
    for (var i = 0; i < years.length; i++) {
        temp[i] = new Editions({
            editions: years[i],
            papers: getPapers(years[i], conf_name),
        });
    }

    return temp;
}

function createNewConf(conferences) {
    var temp;
    temp = new Conference({
        acronym: conferences.acronym,
        fullname: conferences.fullname,
        area: conferences.area,
        subarea: conferences.subarea,
        location: conferences.location,
        year: getEdition(conferences.acronym),
    });

    return temp;
}

my issue is that while I am able to fetch correctly the JSON document from the API, the document looks like the following and I don't know how I can use the '.' on its variables and efficiently loop down the entries of this document to retrieve the relevant variables from the papers.

{
  'search-results': {
    'opensearch:totalResults': '62',
    'opensearch:startIndex': '0',
    'opensearch:itemsPerPage': '25',
    'opensearch:Query': {
      '@role': 'request',
      '@searchTerms': 'CONFNAME(AAAI) AND DOCTYPE(cp) AND SRCTYPE(p) AND PUBYEAR = 2020',
      '@startPage': '0'
    },
    link: [ [Object], [Object], [Object], [Object] ],
    entry: [
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object]
    ]
  }
}

//within each object of the entry array is the following :

"prism:url": "https://api.elsevier.com/content/abstract/scopus_id/85081235453",
        "dc:identifier": "SCOPUS_ID:85081235453",
        "eid": "2-s2.0-85081235453",
        "dc:title": "Structural brain changes with lifetime trauma and re-experiencing symptoms is 5-HTTLPR genotype-dependent",
        "dc:creator": "Ancelin M.L.",
        "prism:publicationName": "European Journal of Psychotraumatology",
        "prism:issn": "20008198",
        "prism:eIssn": "20008066",
        "prism:volume": "11",
        "prism:issueIdentifier": "1",
        "prism:pageRange": null,
        "prism:coverDate": "2020-12-31",
        "prism:coverDisplayDate": "31 December 2020",
        "prism:doi": "10.1080/20008198.2020.1733247",
        "citedby-count": "0",

I have tried various ways, but my biggest problem is the ':' not allowing me to use json_papers.entries.[insert variable here ] notation.

Anyone has any relevant documentation or solution to my problem?

Thank you a lot ! Matthieu Bonnardot


Solution

  • You can try to access the json data using the square brackets notation.

    In your example you're trying to access in dot notation:

    json_papers.entries.[insert variable here ] notation
    

    To use the square brackets notation, you just have to do that:

    json_papers.entries[insert variable here ] notation
    

    As an example, getting your json:

    const a = {
            "prism:url": "https://api.elsevier.com/content/abstract/scopus_id/85081235453",
            "dc:identifier": "SCOPUS_ID:85081235453",
            "eid": "2-s2.0-85081235453",
            "dc:title": "Structural brain changes with lifetime trauma and re-experiencing symptoms is 5-HTTLPR genotype-dependent",
            "dc:creator": "Ancelin M.L.",
            "prism:publicationName": "European Journal of Psychotraumatology",
            "prism:issn": "20008198",
            "prism:eIssn": "20008066",
            "prism:volume": "11",
            "prism:issueIdentifier": "1",
            "prism:pageRange": null,
            "prism:coverDate": "2020-12-31",
            "prism:coverDisplayDate": "31 December 2020",
            "prism:doi": "10.1080/20008198.2020.1733247",
            "citedby-count": "0"}
    

    I was able to access the json data in the following way:

    a["prism:url"] //"https://api.elsevier.com/content/abstract/scopus_id/85081235453"