View function with args - nearprotocol

I've managed to call a view function without args and got the expected result. But when trying to call it with args, I get errors depending on how I call it. I have
args = '{"account_id":"account.testnet"}'
and when calling
await account.viewFunction(
contract,
method,
args
)
I get "Contract method calls expect named arguments wrapped in object, e.g. { argName1: argValue1, argName2: argValue2 }
Thinking it meant an object, I converted args using JSON.parse(args), but now I get the error "Account ID too short". Logging the new args I get:
{account_id: 'account.testnet'}
So I'm guessing the ' is closing the JSON at some point

If you are using PowerShell to call the function try to use a different command-line shell like bash.

I was able to figure out the problem:
I was getting the contract from another file. I set the structure but never properly set the values, so contract was the empty string and thus the error.
Hope this silly mistake is helpful for someone

Related

Cannot call .Count() IEnumerable extensions from lua script using nula

So I have a little lua script where I want to call an extension method on IEnumerable collection.
require ''CLRPackage''
import ''System.Collections.Generic''
import ''System.Linq''
import ''My.Namespace.Containing.AudioMarker''
local audioMarkersWithOffset = GetAudioMarkers();
local numberOfMarkers = audioMarkersWithOffset.Count();
So GetAudioMarkers() is a C# method returning an IEnumerable of AudioMarker objects. Doing a luanet.each will work fine and I will be able to iterate to every element of the collection. But I need the count of that collection and calling .Count() does the following error:
NLua.Exceptions.LuaScriptException: [string "chunk"]:54: attempt to call field 'Count' (a string value).
By the way, I know that with nlua you don't need to pre-register your types to used them so I try with and without the last import about AudioMarker, but got the same result.
I'm probably doing something wrong but I cannot seem to find any documentation on the web that could help regarding this issue.
I have been trying to use the IEnumerable<T>.ToList() extension method myself, but testing reveals that NLua has some problems with generic methods.. Calling a method of the form void func<T>(<T> arg) is possible if you register it as a lua function (Lua.RegisterFunction), but if you try to call the same method on an object present in lua state, you get the "attempt to call method..." error. Also, a method of the form void func<T>(IEnumerable<T> arg) will fail in both cases with a NullReferenceException and the "attempt to call method..." error, respectively.
Another point is, if you want to call C# extension methods from Lua, you need the ":" syntax, not "." (see the NLua "TestExtensionMethods" unit test).

parse.com - cloud code - what do i do with a pointer?

Someone doesn't understand what i want. I want to know what i can do with a pointer.
I have js cloud code.
I have a pointer.
What can i do with it?
Example:
var query = new Parse.Query("Messages"); //POINTER QUERY
console.log(userMessages[0].get("messageId"));
console.log("end2");
query.equalTo("objectId",userMessages[position].get("messageId"));
In the example, userMessages is the result of a prior cloud query.
This line
console.log(userMessages[0].get("messageId"));
helpfully outputs
{"__type":"Pointer","className":"Messages","objectId":"5J4eOletgz"}
This is less useful than you might imagine. I cannot seem to call the objectId from it, and the query
query.equalTo("objectId",userMessages[position].get("messageId"));
query.find ({ ... });
returns nothing. Note that the query should find the pointer-object the pointer-points-to, but instead it helpfully throws the error
Error: 102 bad special key: __type
Which is just about useless.
What can i do with a pointer?
Why don't the people at parse.com bother to write this stuff up anywhere?
That second question is more like a buddhist koan for them to meditate over, no need to respond!
you can use:
userMessagesQuery.include("messageId")
before you execute your query that returns userMessages and you will get the entire object in "messageId" instead of just a pointer.
Also you use
userMessages[0].get("messageId").fetch({success:function(){}})
to get the full object if you don't want to use "include"
Suggestion: I'd rename "messageId" to "message" to make it clear that it's an object pointer and not an ID field.
A pointer is a Parse object with just the minimal data used for linking. If you want to get the rest of the data for a pointer (fully populate it), use the fetch() method.
If you just want the objectId from a pointer, you retrieve it just like you would any other Parse object, by using the myPointer.objectId property.
In your case the following would work, but isn't the most optimal solution:
// I would suggest renaming messageId if you're actually storing pointers
var messagePointer = userMessages[position].get("messageId");
query.equalTo("objectId", messagePointer.objectId);
Instead, as stated by #RipRyness, you could just change your previous query to include() the full object, avoiding many extra queries.
userMessagesQuery.include("messageId");
// ... later in success handler ...
// now a fully populated Message object
var message = userMessages[position].get("messageId");
console.log(message);
Okay so a pointer is does not only point to a certain object, but it can BE that object.
In the query that returns the userMessages result, you can use the line .include("messageId") - this makes your query not only return a pointer to that object, it actually includes that object in place of the pointer.
After using the include statement, userMessages[0].get("messageId") will return the Message object linked that that userMessage object. You no longer need to query the Message collection to get the object.

Debugging a custom function in Google Apps Script

I am trying to create my first custom function for a Google Spreadsheet in Apps Script and I am having a hard time using the debugger.
I am working on the custom function demo code from the Google documentation and I have set a breakpoint in the custom function drivingDistance(origin, destination) that is used in a cell of my spreadsheet. The problem I have is, that that the debugger shows the parameters that are passed into the function as being undefined. The content of any other variables that are created during execution is displayed correctly though (as long as they do not depend on the input parameters).
Funny thing is that although the input parameters are displayed as undefined, the function's calculations succeed, so this seems to be a debugger issue. Unfortunately this problem prevents me from successfully learning to create and debug own code (as I will have to work with complex input parameters).
I have a feeling that the problem is connected to the server-side execution of Apps Script, so I tried to log the input parameters using the Logger class and I also tried to copy these variables into new local variables. But all I came up with was undefined.
Another strange hint is, that typeof of the parameters returns String. But getting the length of them throws an error and trying to concatenate them with another string returns the string "undefined" (see my screen dump).
I am looking for insights about what is going on here.
The debugger is probably not lying to you - if you launch that function in the debugger, it will have no parameters passed to it. No worries, though, you just need to make sure that you get values to use for debugging. Take a look at How can I test a trigger function in GAS?, which demonstrates techniques that can be applied for custom functions.
Instead of defining an event to pass to the function, you'll want to provide (or retrieve from your spreadsheet) values for the parameters.
function test_drivingDistance() {
// Define a set of test values
var testSet = [[ 'Washington, DC', 'Seattle, WA' ],
[ 'Ottawa, ON', 'Orlando, FL'],
[ 'Paris, France', 'Dakar, Senegal']];
// Run multiple tests
for (var test in testSet) {
Logger.log('Test ' + test + ' = ' + drivingDistance(testSet[test][0],testSet[test][1]));
}
// Get parameters from sheet
var TestFromSheet = drivingDistance(ss.getRange('A1').getValue(),ss.getRange('A2').getValue());
}
You get the idea. You can still set breakpoints inside your function, or use debugger to pause execution.
Edit - examining arguments
What arguments is the custom function receiving when called from a spreadsheet?
You're limited in what you can do to debug this, since the debugger can't be used to examine your custom function when invoked from Sheets, and security limitations on custom functions block Logging. It might be enough to get an understanding of argument passing in general. While javascript functions may have named parameters, all arguments are passed as an Array-like object, called arguments. This custom function will return an array that reports the arguments received. When called from a spreadsheet, each argument will appear in its own cell, starting at the cell you enter the function into:
function testArguments( ) {
var argArray = [];
for (var arg in arguments) {
argArray.push("arguments[" + arg + "] = " + JSON.stringify(arguments[arg]))
}
return argArray;
}
In javascript, there aren't really types like int or float - just Number. Those parameters will show up without quotes on them, and look like numbers. Dates arrive as Date objects, but when printed this way show up as Date-y strings. Strings have quotes.
A custom function never receives a range as an argument; when you provide a range parameter in the spreadsheet, its contents are collected into a one or two-dimensional array, and the array is the argument.
You can use this hack to see the structure of the arguments being sent into the custom function:
function TEST(input) {
return (JSON.stringify(input));
}
The results will show up in your sheet like this:

Lambda issue while passing on method

In my application has status array which is something like this
status = ['success','failure','warning']
For example, lambda to process status array like this
print_status = lambda { |stat| puts stat }
When I'm trying to pass lambda on status's each method, I got an error which says "ArugmentError: wrong number of arguments (1 for 0)"
status.each(print_status)
any one help me to fix this issue.
This issue happen due to block and proc mismatch. In your status each method expects block not proc. Ideal solution to convert proc to block is that "&"
status.each(&print_status)
Hope this code may help you to solve your issue

IronRuby: Cannot call method on a COM Object with one or more arguments

When I try and call any method on a COM Object that takes one or more arguments, I get the following error on the last argument:
Could not convert argument 0 for call to Open. (ArgumentError)
Everything works fine when calling a method that takes no arguments, or getting/setting a property. Here is the code that gives me the error above:
def new_com_object(prog_id)
System::Activator.CreateInstance(System::Type.GetTypeFromProgID(prog_id))
end
xls = new_com_object('Excel.Application')
xls.Visible = true
xls.Workbooks.Open('c:\\Book1.xls')
Looks like I need to use String#to_clr_string when calling methods. Right now IronRuby.net documentation is borked, so it's hard to figure that out.

Resources