No output in a transaction response - ethereum

I'm making some tests on web3.py and there is a thing I don't undertand.
I have a contract like this:
contract Test {
function add(uint x, uint y) returns(uint){
return x + y;
}
When i make a transaction on it using
`transaction = eth.sendTransaction({"from": some_address, "to": address_of_the_contract_Test, "data": formated_data})`
and parse the result using
`eth.getTransactionReceipt(transaction)`
it gives me a json-formated response without "output" attribute...
Can someone tell me why ?
(I know that there exist a call function to get the output but I want to do it using a transaction).
Thanks !

Transactions don't have return values. If you want to communicate something back to the client that sent the transaction, you'll probably want to log an event instead.

Related

Why can I only check for "number" in postman tests, but for "integer" when testing against a schema?

I am trying to figure out, why I can't seem to write a test like
pm.expect(responseBody.id).to.be.an("integer");
but apparently have to use "number".
The test fails with the message:
AssertionError: expected 12345 to be an integer
I am sure that it is an integer, though. I've also tried it with other responses.
When I test the response against a schema, where the id is of type "integer", it works.
So, do I only have the possibilty of checking for a "number" in the pm tests or does "number" in this case mean "integer"?
Unfortunately, I haven't found any helpful information on the postman website. Any help would be appreciated!
You're not limited to using just the ChaiJS getters/data-types in the tests to check for an Integer.
I don't know what your response body looks like but I've mocked this test to show how you could use isInteger in the expect statement. This returns a true/false so can be asserted against like this:
let test = { "number": 1234 };
pm.test("Check is an Int - 'With isInteger'", function () {
pm.expect(Number.isInteger(test.number), `Value: ${test.number} is a ${typeof test.number} and not a number`).to.be.true;
});
pm.test("Check is an Int - 'With a number'", function () {
pm.expect(test.number).to.be.a("number");
});

React get json data from http

i am newbie en react technologie.
how can i get the json data from http request ?
as you can see on
i can get the value of console.log(dataExt); from inside this function,
but i can not get the value of console.log(dataExt); from outside this function.
i miss something ?
i did use return dataExt; why i get nothing ?
i have modified my function:
async function getDataExt()
{
try {
let response = await fetch('https://xxxx');
let dataExt = await response.json();
console.log(dataExt);
return dataExt;
} catch(error) {
console.error(error);
}
}
but still i can not get the value
The fetch call is asynchronous and returns a Promise, that's why you need to call then to get the result. As it stands, line 62 will not wait for the fetch in getDataExt() to complete before running the console.log. You need to treat getDataExt as an async function and either do async/await or .then().
It is because of the asynchrony, that is, the "return" is executed when it obtains the value in the request it makes, and that takes time. Let's say it takes 1 second for the data to return, but 0.1 for the variable to print, that means it prints first and then assigns the value. Now, a possible solution would be to create a "state" to save that data or how the partner said, create the function getDataExt as an asynchronous function.

Data binding results in [object Object]

I am trying to bind the records of rows to my view. This is my code on the backend:
[Route("GetCarCount")]
[HttpGet]
public async Task<long> Count()
{
return await _context.Cars.CountAsync();
}
I have test this and it works. I get a result like a number
9
Now I want to bind this on the view this is my code in the service.
getCount() {
return this.http.get(this.baseUrl + 'GetCarCount');
}
And I call this method like this:
GetCarCount() {
this.countrecords = this.carservice.getCount();
}
I have declared a variable like this:
countrecords: any;
Now I bind it like this:
<label>Records count: = {{countrecords}}</label>
It doesn't show the number 9instead I get [object Object]
I have tried like this but it didn't work for me
JSON.parse
JSON.stringify
What am I doing wrong?
Judging from your implementation, countrecords will be of type Observable<number>. Use the async pipe in your template to unwrap its value.
<label>Records count: = {{countrecords | async}}</label>
The countrecords varibales will be a observable.
The http methods like .get or .put are Cold Observables ->
Cold observables start running only upon subscription, i.e., the observable sequence only starts pushing values to the observers when
Subscribe is called. (…) This is different from hot observables such
as mouse move events or stock tickers which are already producing
values even before a subscription is active.
Hence
It's highly probably that the API request is not being made
Moreover in the {{countrecords}} it won't show the value that was returned to you since that holds a subscription (and not the value you'll get on subscribing that subscription)
What I would suggest is
GetCarCount() {
this.carservice.getCount().subscribe((value)=>{this.countrecords=value});
}
and the rest will remain the same.
Hope it helps.

How to return values in Invoke json response

I am trying to design a hyperledger chaincode, that is accessed through a web API, which passes json objects to the code. However, whenever I do an invoke method, I cannot actually return values to the user in the json response.
For instance, here is some sample code:
func (t *TLCChaincode) Invoke(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) {
//Do some stuff
return []byte("Some string"), nil
}
And some sample code for returning an error
func (t *TLCChaincode) Invoke(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) {
//Try to do some stuff
//Get some sort of error
return nil, errors.New("someError")
}
however both of these return a message like this, with the message always being some random character string like below (I suspect a hash of some sort):
{
"jsonrpc": "2.0",
"result": {
"status": "OK",
"message": "1e1123e3-b484-4120-a28e-c3a8db384557"
},
"id": 11
}
As you can see, this response contains neither the response I returned (as in the first case), or the error I returned (in the second case). How would I go about getting the returned bytes, or the returned error into the returned json?
Edit: Please note that if I call an invoke method from another chaincode, it receives the correct return values. It's only when it's returned to the user that it fails to work properly.
“Invoke” is not a synchronous call. Peer generates this OK message immediately when it receives your Web request.
Later, when Validation peers will try to generate new block, this “invoke” method will be executed together with other cached transactions.
In its turn chaincode-to-chaincode calls are synchronous and executed simultaneously.
As a workaround we use another Query request to check the status of this submitted Invoke. It would be great if anybody can propose better solution.
If you need to get a return value as soon as the Invoke is processed (included in a block), your best bet is to use some events (for the moment I guess).
In your chaincode, just setup the event with:
func (stub *ChaincodeStub) SetEvent(name string, payload []byte) error
GoDoc
You may be able to listen for events in your application using the SDK or protobuf messages directly. I'm doing it like this on the developer preview; but it seems that the standard way to retrieve Invoke result is to poll the blockchain via Queries.
There is a related GitHub issue here.

Server polling Angular js and get difference between the response

I have an api getData method which returns json data every minute in angularjs.
function($scope, $http,$timeout, confirm) {
(function tick() {
$http.get('myapi.json').success(function(data) {
$scope.operation = data ;
$timeout(tick, 60000);
})
})();
JSON DATA looks like this.
[{ "requests" :"200"} ....]
After every minute the "requests" value changes. I want to find the difference between the previous request value and current request value. I tried $scope.$watch but doesn't give the object value change. Please let me know how to solve this?