简体   繁体   中英

How to successfully use the callback on an javascript function with an http request

Note: I have limited exp with js so correct me if my I'm completely wrong in how I'm describing this scenario.

I have two javascript files. I am calling a function on the first file (client side) which calls a function on the second file and uses the callback from the second file's function for the purposes of response.success/.error on the first file.

If that doesn't make sense here is some code:

Note: this is being done temporarily using Parse's cloud functions. Let me know if more information is needed regarding those but not sure if it's important.

First file:

Parse.Cloud.define("methodName", function(request, response) {
    ...
    secondFile.myFunction(param1, {
        stuff: request.params.stuff,
    }, function (err, res) {
        if (err) {
            response.error(err);
        } else {
            response.success(res);// I'm assuming this is going to the hardcoded "yes." from httpRequest on second file's function
        }
    });
});

Second File:

myFunction: function(param1, properties, callback) {
        if (!param1) return callback(new Error("Helpful error message"));

        var headersForReq = {
            ...
        };

        var bodyForReq = ...; // the properties properly parsed


        Parse.Cloud.httpRequest({
            method: 'PUT',
            url: ...,
            headers: headersForReq,
            body: bodyForReq,
            success: function (httpResponse) {
                callback(null, 'yes'); // the hardcoded "yes" i referred to
            },
            error: function (httpResponse) {
                callback(httpResponse.status + httpResponse.error);
            }
        });
    }

On my the client, the code is treated as a success (errors aren't thrown or returned back) but when I print out the value it comes across as (null) not "yes".

What's going on here? (Side note, httpRequest is currently not doing anything, its hard to verify if the request is properly being sent because it's being sent to a third party API).

I do know the second file's method is properly being called though. So it's not a silly issue with the module.exports or var secondFile = require('\\path\\secondFile')

I think you are just mis-use the api

Rewrite it with the example style. https://parse.com/docs/js/api/classes/Parse.Cloud.html#methods_httpRequest

    Parse.Cloud.httpRequest({
        method: 'PUT',
        url: ...,
        headers: headersForReq,
        body: bodyForReq
    }).then(function (httpResponse) {
            callback(null, 'yes'); // the hardcoded "yes" i referred to
        },
        function (httpResponse) {
            callback(httpResponse.status + httpResponse.error);
        }
    });

I think below will work, too.

    Parse.Cloud.httpRequest({
        method: 'PUT',
        url: ...,
        headers: headersForReq,
        body: bodyForReq
    }, {
        success: function (httpResponse) {
            callback(null, 'yes'); // the hardcoded "yes" i referred to
        },
        error: function (httpResponse) {
            callback(httpResponse.status + httpResponse.error);
        }
    });

BTW, if you are using open source parse-server, you can use request or request-promise. These 2 npm package is used by many people. (Parse.Promise is not es6-like promise)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM