I got an error message: Property or field 'dal' cannot be found on null when setting up a context variable in watson - watson-assistant

I'm using Cloud Functions to insert data into a table. I copy-pasted the connection credentials from DB2 Warehouse and set up my context variable with them.
All functions work fine outside watson.
"dsn": "DATABASE=BLUDB;HOSTNAME=dashdb-entry-yp-dal09 08.services.dal.bluemix.net;
I should be able to connect and insert the data into the table.
Please help.
This is the code I use (provided by the tutorial):
function insertClient(dsn, eventValues) {
try {
var conn = ibmdb.openSync(dsn);
// The timestamp value is derived from date and time values passed in
var data = conn.querySync("insert into client (name) values('test')", eventValues);
conn.closeSync();
return {
result: data
};
} catch (e) {
return {
dberror: e
}
}
}
function main({
eventValues,
__bx_creds: {
dashDB: {
dsn
}
}
}) {
return insertClient(dsn, eventValues);
}
Credentials provided by DB2:
{
"hostname": "dashdb-entry-yp-dal09-08.services.dal.bluemix.net",
"password": "",
"https_url": "https://dashdb-entry-yp-dal09-08.services.dal.bluemix.net:8443",
"port": 50000,
"ssldsn": "DATABASE=BLUDB;HOSTNAME=dashdb-entry-yp-dal09-08.services.dal.bluemix.net;PORT=50001;PROTOCOL=TCPIP;UID=dash100113;PWD=;Security=SSL;",
"host": "dashdb-entry-yp-dal09-08.services.dal.bluemix.net",
"jdbcurl": "jdbc:db2://dashdb-entry-yp-dal09-08.services.dal.bluemix.net:50000/BLUDB",
"uri": "db2://dash100113:Qs1_I_5eZUkl#dashdb-entry-yp-dal09-08.services.dal.bluemix.net:50000/BLUDB",
"db": "BLUDB",
"dsn": "DATABASE=BLUDB;HOSTNAME=dashdb-entry-yp-dal09-08.services.dal.bluemix.net;PORT=50000;PROTOCOL=TCPIP;UID=dash100113;PWD=;",
"username": "dash100113",
"ssljdbcurl": "jdbc:db2://dashdb-entry-yp-dal09-08.services.dal.bluemix.net:50001/BLUDB:sslConnection=true;"
}

Related

Parsing JSON in Office Scripts - not iterable

I am attempting to parse JSON in Office Scripts that prints the headings and row information on the sheet. I'm successfully fetching the data, but I keep getting the error message that my information is not iterable on the "for" line.
async function main(workbook: ExcelScript.Workbook) {
// Call the API
const response = await fetch('WEBADDRESSHERE');
const sitedata: siteInformation[] = await response.json();
// Set the types for rows
const rows: (string | number)[][]=[];
// Iterate through the data and get the row headings
for (let site of sitedata){
rows.push([site.SiteID, site.SiteDescription, site.EffectiveDate, site.DataPresent]);
}
// Get the current sheet
const sheet = workbook.getActiveWorksheet();
// Set the range to start writing the details
const range = sheet.getRange('A2').getResizedRange(rows.length - 1, rows[0].length - 1);
range.setValues(rows);
return;
}
interface siteInformation {
SiteID: string;
SiteDescription: string;
EffectiveDate: date;
DataPresent: string;
}
This is the sample JSON I'm working with.
{
"1": {
"SiteID": "2",
"SiteDescription": "SiteA",
"EffectiveDate": "2022-08-01",
"DataPresent": "Yes"
},
"2": {
"SiteID": "2",
"SiteDescription": "SiteA",
"EffectiveDate": "2022-08-02",
"DataPresent": "Yes"
}
}
There are a few things going on here
I believe the reason you're getting the "not iterable" error is because you're using the wrong loop. To loop through a JSON dictionary, you need to use for...in... not for...of...
Second, when you update the loop, you'll get the key back as the element. So you need to use the key with the sitedata to get the specific JSON element you're trying to loop through.
Lastly, although it's not necessary, you listed the date type as a type in the interface. This is showing an error on the office scripts IDE for me. So you may want to update that to string.
You can see a code snippet with the updates below:
async function main(workbook: ExcelScript.Workbook) {
// Call the API
const sitedataStr = `{
"1": {
"SiteID": "1",
"SiteDescription": "SiteA",
"EffectiveDate": "2022-08-01",
"DataPresent": "Yes"
},
"2": {
"SiteID": "2",
"SiteDescription": "SiteA",
"EffectiveDate": "2022-08-02",
"DataPresent": "Yes"
}
}`
let sitedata: siteInformation = JSON.parse(sitedataStr)
// Set the types for rows
const rows: (string | number)[][] = [];
// Iterate through the data and get the row headings
for (let key in sitedata) {
let site:siteInformation = sitedata[key]
rows.push([site.SiteID, site.SiteDescription, site.EffectiveDate, site.DataPresent]);
}
}
interface siteInformation {
SiteID: string;
SiteDescription: string;
EffectiveDate: string;
DataPresent: string;
}

get json root key based on value

{
"vitals": {
"title": "Vitals IR",
"name": "vitalsIr",
"formid":"5ed5f7ca158a91827891cab2"
},
"anthropometry": {
"title": "Anthropometry IR",
"name": "anthropometryIr",
"formid":"5ed621ac158a91228191cafd"
}}
How to get the root key name vitals , anthropometry based on formid value
for example if formid value is "5ed621ac158a91228191cafd"
I need output as anthropometry need to achieve this in angular javascript
To loop through a json object using JavaScript, you can preform a for on the keys. E.g for (jsonPropertyName in json) { ... }. You can then query the formId.
For example:
function ProvideRootKey(formId) {
var results;
for (jsonPropertyName in json) {
if (json[jsonPropertyName]['formid'] === formId) {
results = jsonPropertyName
}
}
return results
}
var json = JSON.parse('{ "vitals":{ "title":"Vitals IR", "name":"vitalsIr","formid":"5ed5f7ca158a91827891cab2"}, "anthropometry":{ "title":"Anthropometry IR", "name":"anthropometryIr", "formid":"5ed621ac158a91228191cafd"}}');
console.log(ProvideRootKey('5ed621ac158a91228191cafd'))
Output (in console log):
"anthropometry"
See working example: https://jsfiddle.net/vgo81stm/1/

TypeError: Cannot read property 'reduce' of undefined in react

I have a form in which I am asking the user to input field values for a couple of fields, storing the field values in an state and displaying the state values in a customised format.
So, I have a couple of input fields and a submit button:
<button onClick={this.handleSubmit}>Submit</button>
{
this.state.credentials &&
//<Credentials value={this.state}/>
<Credentials value={JSON.stringify(this.state, undefined, 2)} />
}
The Credentials function convert the state of the component in JSON format:
const Credentials = ({value} ) => {
return <pre>{formatState(value)}</pre>;
}
The formatState function will basically manipulate the state values and display them in the way I want:
function formatState(state) {
console.log("hi")
console.log(state);
const output = state.groups.reduce((final, s)=> {
console.log(output)
const values = Object.keys(s).reduce((out, o)=> {
out[o] = s[o].map(k => Object.values(k))
return out;
}, {})
final = {...final, ...values}
return final;
}, {})
console.log(output)
}
The state looks like this:
{
"groups": [
{
"typeA": [
{
"name": "abc"
},
{
"number": "13,14"
}
],
"typeB": [
{
"country": "xyz"
},
{
"date1": "2019-05-14"
}
]
}
]
}
But I want the output like this:
groups: {
"typeA": [[abc],[13,14]],
"typeB": [[2019-05-14],[xyz]]
}
SO, reduce function is used to convert the state into the following output. But I getting the error :
"TypeError: Cannot read property 'reduce' of undefined"
Please can anybody tell me why this is happening.
Error is here <Credentials value={JSON.stringify(this.state, undefined, 2)} />. JSON.stringify produces string representaion of some object (this.state in your case). Argument state of formatState has type of string. It seems that you want to have state arguemnt to be object. So you should do
<Credentials value={this.state} />

How to access data inside a complex JSON object in Dart?

I use a WebSocket to communicate to a server in my Flutter app. Let's say I receive a JSON object trough the WebSocket :
{
"action": "getProduct",
"cbackid": 1521474231306,
"datas": {
"product": {
"Actif": 1,
"AfficheQte": 0,
"Article": "6"
},
"result": "success"
},
"deviceID": "4340a8fdc126bb59"
}
I have no idea what the content of datas will be until I read the action, and even then, it's not guaranteed to be the same every time. One example of a changing action/datas is when the product doesn't exist.
I can parse it in a Map<String, Object>, but then, how do I access what's inside the Object?
What's the correct way to read this data?
Not sure what the question is about, but you can check the type of the values and then continue accordingly
if(json['action'] == 'getProduct') {
var datas = json['datas'];
if(datas is List) {
var items = datas as List;
for(var item in items) {
print('list item: $item');
}
} else if (datas is Map) {
var items = datas as Map;
for(var key in items.keys) {
print('map item: $key, ${items[key]}');
}
} else if(datas is String) {
print('datas: $datas');
} // ... similar for all other possible types like `int`, `double`, `bool`, ...
}
You also can make that recursive to check list or map values if they are String, ...

IBM Worklight Adapter parsing JSON

{
"isSuccessful": true,
"resultSet": [
{
"name": "pradeep",
"password": 123,
"timestamp": "2014-04-08T12:58:45.000Z"
},
{
"name": "dileep",
"password": 1234,
"timestamp": "2014-04-08T13:00:52.000Z"
}
]
}
This invocation result i have got by using Sql adapter so how to parse this invocation result and how can i display name,password,timestamp from this JSON object.Do i need to use HTTP Adapter.
If you want to get the length of results you should use result.invocationResult.resultSet.length which will give you the total number of results, where items is the response coming from the adapter and invocationResult contains the results and other paramaters, from which you will have to access the results for accessing only the particular output.To get value call
result.invocationResult.resultSet.name[position]
Like that call all the fields password,timestamp with position
in for loop
function handleSuccess(result) {
var invocationResult = result.invocationResult;
var isSuccessful = invocationResult.isSuccessful;
if (true == isSuccessful) {
var result = invocationResult.resultSet;
for ( var i = 0; i < result.length; i++) {
alert(result.name[i]);
alert(result.password[i]);
alert(result.timestamp[i]);
}
} else {
alert("error");
}