I'm doing KPI's in node-red, and I'm using node-red-node-firebird to connect my database and get the results from it. For that I made a query to select the columns I need, one of those is:
NAME Varchar(40), with an example value: "Pizzas"
(example: Select NAME from mytable)
When I receive the query response on node-red, I store it inside the msg.payload. The problem is the result that I get it isn't the string "Pizzas" but a buffer "NAME":{"type":"Buffer","data":[80,105,122,122,97,115]}}.
How can I get the string and not the buffer?
I already tried a lot of things, among them:
On the query I have tried cast(NAME AS varchar(40)) AS NAME; [NAME] without success. Put msg.payload.data.toString('utf-8') in function node but nothing happens, the function:
var objectData = msg.objectData; //this is the query response
//------------Columns----------------------------
var fields = [];
var i = 0;
if(objectData.length > 0) {
var data = objectData[0];
for(var key in data) {
fields[i] = key;
i++;
}
//TRY nº1
objectData.foreach(function(obj){
if (Buffer.isBuffer(obj) === true) {
obj = obj.toString('utf-8');
}
})
}
//-----------------------------------------
msg.method = "POST";
msg.url = //My api request//;
msg.headers = {};
msg.headers["Content-Type"] = "application/json";
msg.headers.Authorization = //auth//;
msg.payload = {
'groupID': 'Group123',
'companyID': 1221,
'table': 'DemoTable',
'fields': fields,
'data': objectData, //problem
'delete': true,
};
//TRY nº2
msg.payload = msg.payload.data.toString('utf-8');
return msg;
I solved my problem changing the select to:
SELECT cast(name as varchar(100) character set win1252) NOME FROM mytable
Thanks for the help :)
Is it possible to create a comptime function in zig that would generate a new struct type? The function would receive an array of strings and an array of types. The strings are the names of subsequent struct fields.
This has been implemented now as https://github.com/ziglang/zig/pull/6099
const builtin = #import("std").builtin;
const A = #Type(.{
.Struct = .{
.layout = .Auto,
.fields = &[_]builtin.TypeInfo.StructField{
.{ .name = "one", .field_type = i32, .default_value = null, .is_comptime = false, .alignment = 0 },
},
.decls = &[_]builtin.TypeInfo.Declaration{},
.is_tuple = false,
},
});
test "" {
const a: A = .{ .one = 25 };
}
The TypeInfo struct is defined here.
Partially. This has been long proposed at https://github.com/ziglang/zig/issues/383
You can only do so with fields, not with custom decls.
There are two realm object lists, one of them is managed realm object and another one is unmanaged realm object.
I referred these links Comparing realm object list and Testing for equality but no luck
My realm object looks like this
#objcMembers
class MetaData: Object, Codable {
dynamic var label: String?
dynamic var category: String?
var value = List<Value>()
}
#objcMembers
class Value: Object, Codable {
dynamic var key: String?
dynamic var val: String?
}
Here is the realm object used for comparison
[0] MetaData {
label = home;
category = Address;
value = List<Value> <0x61000010f5d0> (
[0] Value {
key = street;
val = 1001 Leavenworth Street;
},
[1] Value {
key = country;
val = USA;
},
[2] Value {
key = city;
val = Sausalito;
},
[3] Value {
key = state;
val = CA;
},
[4] Value {
key = postalCode;
val = 94965;
}
);
},
I tried overriding equality and hashValue which gives me false even though object lists are same. Any suggestions for comparing?
This is my first time playing with a JSON API and my java is super rusty.
I'm trying to do the following:
Pull a string from a Google Sheets cell into function getSymbol (name). In this case, the string should be "Ethereum"
Insert the name variable into a url string, which is where the JSON I want to pull lives. In this case, the API output looks like this:
[
{
"id": "ethereum",
"name": "Ethereum",
"symbol": "ETH",
"rank": "2",
"price_usd": "95.3675",
"price_btc": "0.0605977",
"24h_volume_usd": "152223000.0",
"market_cap_usd": "8713986432.0",
"available_supply": "91372705.0",
"total_supply": "91372705.0",
"percent_change_1h": "0.38",
"percent_change_24h": "1.38",
"percent_change_7d": "37.07",
"last_updated": "1494105266"
}
]
Next I want to pull the "symbol" item from the JSON and return that back to the spreadsheet. In this case, the function would take in "Ethereum" and return "ETH".
Below is my code. Whenever I run it I get an error saying my name variable is undefined, resulting in a URL that looks like this (says "undefined" instead of "ethereum"):
https://api.coinmarketcap.com/v1/ticker/undefined/?convert=USD
What am I doing wrong?
function getSymbol (name) {
var url = "https://api.coinmarketcap.com/v1/ticker/"+name+"/?convert=USD";
var response = UrlFetchApp.fetch(url);
var text = response.getContentText();
var json = JSON.parse(text);
var sym = json["symbol"];
return sym;
}
The return type is an array of objects i.e. json = [{object1},{object2}]
Even though there is just one element, you still need to access it like so
var sym = json[0]["symbol"]
//or
var sym = json[0].symbol
Your final code will look like this:
function getSymbol (name) {
var url = "https://api.coinmarketcap.com/v1/ticker/"+name+"/?convert=USD";
var response = UrlFetchApp.fetch(url);
var text = response.getContentText();
var json = JSON.parse(text);
var sym = json[0]["symbol"];
return sym;
}
I have 2 highcharts like this:http://jsfiddle.net/bqnkeLx9/
And I want to be able combine them into something like this: http://jsfiddle.net/gy2yo184/
Basically, is there an easy way to join 2 arrays together based on another array?
var category1 = [1,2,4,6,9]
var arr1 = [1,2,3,4,5]
var category2 = [1,3,6,8]
var arr2 = [6,7,8,9]
//do something to end with:
var allcategories = [1,2,3,4,6,8,9]
arr1 = [1,2,0,3,4,0,5]
arr2 = [6,0,7,0,8,9,0]
or maybe there is an easier way to plot this?
I don't think there is an easy way to do it. Or rather easier than:
making unique array of categories
manually filling up array with 0 values, when category is missing
For example:
// union without diplicates:
// taken from:
// http://stackoverflow.com/questions/1584370/how-to-merge-two-arrays-in-javascript-and-de-duplicate-items
Array.prototype.unique = function() {
var a = this.concat();
for(var i=0; i<a.length; ++i) {
for(var j=i+1; j<a.length; ++j) {
if(a[i] === a[j])
a.splice(j--, 1);
}
}
return a;
};
And now use case:
// arrays:
var cats_1 = [1,2,4,6,9],
cats_2 = [1,3,6,8],
data_1 = [1,2,3,4,5],
data_2 = [6,7,8,9],
data_1_modified = [],
data_2_modified = [],
categories;
// get cateogries
categories = cats_1.concat(cats_2).unique();
// you may want to sort categories
categories.sort(function(a, b) {
return a - b;
});
categories.map(function(e, i) {
var index;
if( (index = cats_1.indexOf(e)) == -1) {
data_1_modified.push(0); // missing category
} else {
data_1_modified.push(data_1[index]);
}
if( (index = cats_2.indexOf(e)) == -1) {
data_2_modified.push(0); // missing category
} else {
data_2_modified.push(data_2[index]);
}
return e;
});
$('#container1').highcharts({
xAxis: {
categories: categories
},
series: [{
data: data_1_modified
}, {
data: data_2_modified
}]
});
And live demo: http://jsfiddle.net/bqnkeLx9/1/
you can Use javascript function concat() to join two array.
assuming here is our arrays
var category1 = [1,2,4,6,9]
var arr1 = [1,2,3,4,5]
var category2 = [1,3,6,8]
var arr1 = [1,2,3,4,5]
var category = category1.concat(category1);
the elements of array category will be something like as below
[1,2,4,6,9,8]
now you can pass array in highchart
Update
After removing all duplicate elements and after sorting final array is stored in arrayuniqueCat
$(function () {
var category1 = [1,2,4,6,9];
var category2 = [6,3,1,8];
var allcategories = category1.concat(category2);
var arr1 = [2,2,0,3,4,0,5];
var arr2 = [6,0,7,0,8,9,0];
var uniqueCat = allcategories.filter(function(elem, index, self) {
return index == self.indexOf(elem);
}).sort();
$('#container1').highcharts({
xAxis: {
categories: uniqueCat
},
series: [{
data: arr1
},{
data: arr2
}]
});
});
fiddle