Value not reflecting in couchdb using putState in Hyperledger Fabric - node.js

I am trying to build a chaincode application using fabric-shim in NodeJS. When I use the putState(key,Buffer.from(value)), the value does not reflect in CouchDB, while key and other parameters (stub etc..) can be seen in CouchDB.
Any erason why it does not work?

Yes, if you look at CouchDB document, it won't show the value of a respective key. But you can see there is one attachment with a document which has actually byte array or buffer value of a key.
Please check the following image to see example attachment, if you click on it will get download as a binary file.
If you wanna fetch exact value then use the following script to get the database dump
https://github.com/danielebailo/couchdb-dump
So after database dump, you will get to see JSON object with data as one of the key in it as follows
{
"_id": <<id>>,
"_rev": <<rev>>,
"~version": <<version>>,
"_attachments": {
"valueBytes": {
"content_type": "application/octet-stream",
"revpos": 1,
"digest": <<key>>,
"data": "AAAHAQA=" ==> <<value in Base64 format>>
}
}
}
Using Base64 decoder you can decode value which will return value in form of byte array or buffer. Then you can convert byte array or buffer to respective object.

Related

Firebase real time database returning a null value, TypeError: row is null

What I am trying to do is to get the data from the firebase realtime database through a nodejs application and from there allow the frontend to make requests directly to my node application and not to the firebase. I made the request to the firebase database, just like the code below but it returns a null value and when the frontend reads this value it generates an error. I already checked in the firebase database and there is no null value. The firebase returns all data as it should, but it always returns this "null", just like the image below.
{
"data": [
null,
{
"data": "12/04/2016",
"subtitulo": "test...",
"titulo": "test"
},
{
"data": "05/05/2019",
"subtitulo": "test...",
"titulo": "test"
},
{
"data": "02/01/2020",
"subtitle": "title",
"title": "test"
}
]
}
Node Js code:
db.ref('myreference/here/').once('value', (snapshot) => {
data = snapshot.val();
return res.json({
data,
});
});
Error generated by React Js when I request the api:
While the Firebase Realtime Database doesn't natively store arrays, it's APIs and SDKs try to handle arrays in the source data gracefully. When you store an array, they convert it to a JSON map with sequential, numeric keys ("0", "1", etc). And then you read such a structure back from the database, they convert it to an array.
That last action is what you see in your code: since your keys look like array indexes, the client converts the data to an array. And since key "0" is missing, it inserts a null element in the array for that.
The easiest way to circumvent this array coercion is by add a prefix to your keys, like:
"Avisos": {
"key1": { ... },
"key2": { ... },
"key2": { ... },
}
That will ensure the Firebase API/SDK doesn't coerce the data into an array.
In general, I recommend not using sequential numeric keys (Kato gives a good overview in Best Practices: Arrays in Firebase), but instead use the keys generated by Firebase's push() method.

In azure logic app how to get key and value of url encoded data

In azure logic app how to get key and value of URL encoded data. I am not finding details regarding this.
I found way to work with application/x-www-url-formencoded data.
{
"$content-type": "application/x-www-form-urlencoded",
"$content": "<base64EncodedContent>",
"$formdata": [{
"key": "key1",
"value": "value1"
}
to get key in code view #triggerBody()['$formdata'][0]['key'].
to get value in code view #triggerBody()['$formdata'][0]['value'].
Some content types are supported and work with logic apps, but might require manually retrieving the message body by decoding the $content.
For example, suppose you trigger an application/x-www-url-formencoded request where $content is the payload encoded as a base64 string to preserve all data.
Because the request isn't plain text or JSON, the request is stored in the action as follows:
"$content-type": "application/x-www-form-urlencoded",
"$content": "<Base64EncodedContent>",
"$formdata": [{
"key": "ToCountry",
"value": "AU"
}
Being this a Form Data Post request, we can use the function #triggerFormDataValue() to get each of the properties, e.g. #triggerFormDataValue(‘Body’) and #triggerFormDataValue(‘From’).
For more details, you could refer to this blog.

CouchDB View not returning documents when ?key specified

I'm coming to CouchDB from an SQL background and am trying to do the common "SELECT FROM DB where field = someValue". I've created the following design document:
{
"_id": "_design/views",
"views": {
"byRubric": {
"map": "function(doc) {if(doc.idRubric){emit(doc._id, doc.idRubric);} }"
}
}
}
If I query the CouchDB table using the following URL, it correctly returns all 15 documents in the table:
http://localhost:5984/rubric_content/_design/views/_view/byRubric
If, however, I try to get those documents in this view which have a particular value in the idRubric field, one which I know is present by, for example, executing the following url, I get 0 documents back when, in fact, 12 of the 15 documents have this specific value in the idRubric field. http://localhost:5984/rubric_content/_design/views/_view/byRubric?key="9bf94452c27908f241ab559d2a0d46c5" (no, it doesn't make any difference if the " marks are replaced by %22). The URL does fail if I leave the quote marks off.
What am I missing? Running this locally for test on OSX 10.12.3 using couchdb - Apache CouchDB 1.6.1
Your view is emitting the document with the document with the id as the key.
Instead, you want to emit the the rubricID as the key.
{
"_id": "_design/views",
"views": {
"byRubric": {
"map": "function(doc) {if(doc.idRubric){emit(doc.idRubric);} }"
}
}
}
Then, the query will be the following :
http://localhost:5984/rubric_content/_design/views/_view/byRubric?key="rubric3"
When you use a map, you need to think as if it was a dictionnary. You have a key and a value. You will search for a matching key and get the value.
If you don't emit any value, you can simply use the ?include_docs=true parameter to get the entire document.

Looking for a REST API with template based fake data (NodeJS + key-value store)

I want to be able to write a config like this...
{
'collection' : 'payments',
'rows' : 100,
'template' : {
"id" : "1...100000",
"status" : ["None", "SentToPayer", "Overdue", "Completed"],
"amount" : ["100","200","500"]
}
}
...and it would create a key-value pair collection in a key-value store (maybe MongoDB) with 100 rows like this:
{
"id": "80494",
"status": "None",
"amount": "200"
}
And the data would be fully accessible and editable trough a REST API.
GET http://server/payments/80494 would likely get me the node above.
I am pretty sure I've seen something like this before, but I'm not able to find it right now. Does anyone know something that gives me what I want?
I ended up creating my own thingy.
This one puts data in an MongoDB database.
https://github.com/janjarfalk/datafixture.js
This one expose it.
https://github.com/tdegrunt/mongodb-rest
You can try json-server to fake a REST API on the server side, or FakeRest to fake a REST API on the client side.

Get a single value from document

Is there a possibility in CouchDB to retrieve a single value from a document? For example, I have a document with 10000 keys like this:
{
"_id": "8098091cc795acde43cd45335373cc92",
"_rev": "2-6d2e0ac43618388cc958b91e5015bba5",
"1":"some value",
"2":"another value",
...
"10000":"last value"
}
and I don't want to parse the whole document each time but just want to get a value of key "2". Is this possible?
EDIT:
Of course, before asking this question I looked through the docs and googled around this but haven't found such possibility. But I'm wondering why this is not possible? In my understanding it is not too much difference between view and document since both of them are sets of key-value pairs, but it is possible to get single value from view using ?key=... in the query string and not possible to do the same for document. Why?
You can use a CouchDB show function to process a document on the server and send arbitrary data to the client. Show functions can do simple things like extracting a single value from the document; or they can convert to a completely different content-type such as XML.
For example:
In database db
which has design document _design/example with show function one_field
you want to fetch document with ID doc_id
but only retrieve the field some_field which has a value "I am the some_field value!"
That query would be:
GET /db/_design/example/_show/one_field/doc_id?name=some_field
The show function would look like this:
function(doc, req) {
// _show function to extract one field
var field_name = req.query.name; // Set to e.g. "some_field"
var field_value = doc[field_name];
return { "code": 200 // HTTP status code
, "headers": // Response headers
{ "content-type": "text/plain" }
, body: field_value
};
}
The design document would look like this:
{ "_id": "_design/example"
, "shows":
{ "one_field": ... } // Paste the above function as a string.
}
CouchDB will return
HTTP 200 OK
Content-Type: text/plain
Content-Length: 26
I am the some_field value!
No. CouchDB is a document oriented DB, so it doesn't support more granular access to the data beyond the overall document itself.
It gives you fine grained indexing, but not actual access.

Resources