1

I am working on a little project using Node js in which I have to access Musicbrainz API and get some data from it.

The thing is that I'm trying to get a value from the API using a URL, then gets the XML version of the resulting page and finally gets the value I want (which is "count") using the XMLDOM package for node js (https://www.npmjs.com/package/xmldom).

The XML looks like this and I want to get the value of count (underlined in red): enter image description here

This is the function I'm using to get the value:

// Loading of necessary modules and creation of a new application.
var express = require("express"); 
var app = express();
var request = require("request");
var DOMParser = new (require('xmldom')).DOMParser;

// URL of the musicbrainz API.
var root_url = "https://musicbrainz.org/ws/2/";

// Example: MBID = 0da580f2-6768-498f-af9d-2becaddf15e0
function getReleases(MBID){

    var releases_URL = root_url + "release-group/" + MBID + "?inc=releases"
    var count
    request({
        headers: {
            'User-Agent': 'my-musicbrainz-client',
        },
        url: releases_URL,
        json: false
    }, async function (error, response, body) {
    
        if (!error && response.statusCode === 200) {
            var document = DOMParser.parseFromString(body);
            var x = document.getElementsByTagName("release-list")
            console.log("Release list = "+ x)

            count = x.getAttribute('count')  
            console.log("COUNT:"+ y)                                                                  
            return count
        }else{
            console.log("ERROR IN FUNCTION")
        }
    })

}

But the problem is that count = x.getAttribute('count') is not working as it throws UnhandledPromiseRejectionWarning: TypeError: x.getAttribute is not a function. How is this possible since I installed XMLDOM package and the function getAttribute exists? Am I using it wrong? Is there another way to get the "count" value?

Thanks!

1 Answer 1

1

document.getElementsByTagName returns a node list so x[0] could be an element node for which you can call getAttribute if the element(s) are found. In the world of the W3C DOM APIs I think it would be safer to use getElementsByTagNameNS as your elements are in a namespace but I don't know that Node.js API to tell whether you need it there.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This solved the problem completely!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.