How to handle the response object in Custom Script

In my scenario, the users are linked to warehouses. So there is a custom link field for User. In Sales Order, I need to change items’ warehouse to user’s warehouse. Right now the only way I found to get the user’s warehouse is the function frappe.db.get_value.

To make it easy to understand and to exclude other possible problems, here I will take fetching the user’s full_name instead of warehouse as an example.

I put the test script in Custome Script as following:

var req = frappe.db.get_value(“User”, {“name”:frappe.session.user}, “full_name”);
console.log(req);
var jsonResponse = req.responseText;
console.log(jsonResponse);

In the console it shows req is a response object:

{…}
abort: function abort()
always: function always()
complete: function add()
done: function add()
error: function add()
fail: function add()
getAllResponseHeaders: function getAllResponseHeaders()
getResponseHeader: function getResponseHeader()
overrideMimeType: function overrideMimeType()
pipe: function then()
progress: function add()
promise: function promise()
readyState: 4
responseJSON: {…}
message: Object { full_name: “Alvin Chen” }
proto: Object { … }
responseText: “{“message”:{“full_name”:“Alvin Chen”}}”
setRequestHeader: function setRequestHeader()
state: function state()
status: 200
statusCode: function statusCode()
statusText: “OK”
success: function add()
then: function then()
proto: Object { … }

The returned object is right, and the right information is there. The problem is that I can not get the property with member operator.
The output of console.log(jsonResponse) is “undefined”.
Actually any other member is also not accessible.

How can I access the members of the response?

Structure this as a callback instead.

frappe.call({
    method:  frappe.db.get_value,
    args: {“User”, {“name”:frappe.session.user}, “full_name”},
    doc: frm.doc,
    callback: function(r) {
        if (!r.exc) {
            // code snippet
        }
    }
});
1 Like

@tmatteson Thanks for your answer!
I were trying to figure out the syntax of frappe.call. Now there is no syntax error, but no right response.
my script is as following(callback is not included since that segment can not reach):

frappe.call({
method:“frappe.db.get_value”,
args:{
doctype:“User”,
filter:{name:frappe.session.user},
filedname:“full_name”,
}
})

The result of this script is “Not found
The resource you are looking for is not available”.

Any idea about this type of error(or logic error)?

Thanks again for your help!

Here’s a better example than the one I provided:
Client side

If you could explain what you’re trying to accomplish (beyond getting this code working), I think I could give better advice.

Thanks, @tmatteson, the problem lies in that the method listed does not exist. The right method is frappe.client.get_value.

    frappe.call({
        method: "frappe.client.get_value",
        args: {
            "doctype": "User",
    "filters": {"name": frappe.session.user},
        "fieldname": "full_name"
        }
1 Like

Sorry, the frappe.db.get_value call is the server side version; it takes the same arguments.
Thanks for posting the solution.