JSON Datasource: mapToTextValue not found

It has to be something dumb but I can’t see it

The following code snippet throws.

hostReportError.js:3 Uncaught TypeError: Cannot read properties of undefined (reading ‘mapToTextValue’)
at eval (datasource.js:228:13)

The returned data from dorequest

{“status”:200,“statusText”:“OK”,“ok”:true,“data”:{“results”:[{“servername”:“frontoff”},{“servername”:“backoff”},{“servername”:“frontoff_er”},{“servername”:“backoff_er”}],“hasMore”:false,“responseTime”:2},“headers”:{},“url”:“https://xxxxxxxx:3000/api/datasources/proxy/1/sql",“type”:“basic”,“redirected”:false,“config”:{“url”:“api/datasources/proxy/1/sql”,“data”:"{“sql”:“select servername from oni_server where custid = 14”}”,“method”:“POST”,“headers”:{“Content-Type”:“application/json”,“X-Grafana-Org-Id”:1},“retry”:0,“hideFromInspector”:false}}

Via Chrome dev tools, ‘this’ is not defined when I get to the return this.mapToTextValue

{
key: ‘metricFindQuery’,
value: function metricFindQuery(query) {
console.log("\n Entry metricFindQuery - query : \n" + JSON.stringify(query));

                    var interpolated = {
                    target: this.templateSrv.replace(query, null, 'regex')
                    };

                    var str = new Object;
                    str.sql = interpolated.target;
                    var mypost = JSON.stringify(str);

                    return this.doRequest({
                        url: this.url + "/sql",
                        data: mypost,
                        method: 'POST'
                    }).then(function(result) {
                        console.log("\n Exit metricFindQuery - result : \n" + JSON.stringify(result));
                        return this.mapToTextValue;   **<<<< Crashes**
                    });
                }
            }, {
                key: 'mapToTextValue',
                value: function mapToTextValue(result) {
                    console.log('Boo');
                    console.log('mapToTextValue' + JSON.stringify(result));
                    return _.map(result.data, function(d, i) {
                        if (d && d.text && d.value) {
                            return {
                                text: d.text,
                                value: d.value
                            };
                        } else if (_.isObject(d)) {
                            var col = Object.keys(d)[0];
                            return {
                                text: d[col],
                                value: i
                            };
                        }
                        return {
                            text: d,
                            value: d
                        };
                    });
                }
            },

Can I ask which plugin you’re referring to by “JSON data source” (there are several JSON data sources)?

The ‘SimpleJSON’ downloaded from Grafana. Got it working, was being dumb

Changed the metricFindQuery return to

return this.doRequest({
url: this.url + “/sql”,
data: mypost,
method: ‘POST’
}).then(this.mapToTextValue);
}

1 Like

Glad that you got it working. Thanks for sharing the solution!

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.