如何从另一个函数读取返回给 Twilio 函数的响应?

How to read the response returned to a Twilio function from another function?

非常基本的问题 - 我正在从另一个 twilio 函数调用一个 twilio 函数来检索 salesforce 记录的 ID。以下代码是从一个函数返回到另一个函数的内容。我只是想“阅读”响应的内容以获取 ID,但似乎无法弄清楚。我确认返回响应的函数工作正常(包含正确的数据。)

非常感谢您的帮助!

responsebody
2020-11-24T19:38:02.642Z    13c8fae2-5f74-40c2-942a-d6aa7ed85c48    INFO    Response {
  size: 0,
  timeout: 0,
  [Symbol(Body internals)]:
   { body:
      PassThrough {
        _readableState: [ReadableState],
        readable: true,
        _events: [Object],
        _eventsCount: 2,
        _maxListeners: undefined,
        _writableState: [WritableState],
        writable: false,
        allowHalfOpen: true,
        _transformState: [Object] },
     disturbed: false,
     error: null },
  [Symbol(Response internals)]:
   { url: 'https://coxczsdlk-dffcat-8307.twil.io/sf-get-record',
     status: 200,
     statusText: 'OK',
     headers: Headers { [Symbol(map)]: [Object] },
     counter: 0 } }

这是返回此响应的函数的代码 - 这一行 returns 正确的 ID - response.body.records[0].Id

const querystring = require('querystring');
const request = require('request');
let globalCallback;

exports.handler = function(context, event, callback) {
    globalCallback = callback;
    console.log("Starting");
    console.log("event: ", event);
    run(context.DOMAIN_NAME, event);
};

function run(domain, event){
    request({
        uri: `https://${domain}/sf-access-token`,
        method: 'GET'
    }, function (err, res, body) {
        if(res.statusCode == 200){
            // Received Access Token. Now build and send the request
            processRequest(JSON.parse(body), event);
        } else{
            globalCallback(`Error getting token: ${res.body}`);
        }
    });
}

function processRequest(sfAuthReponse, event){
   // if(validateRequest(event)) {
        var options = {
            // uri: `${sfAuthReponse.instance_url}/services/data/v43.0/query/?q=SELECT+id+From+${event.objectAPIName}+WHERE+callSID__c='${event.callSID}'`,
            uri: `${sfAuthReponse.instance_url}/services/data/v43.0/query/?q=SELECT+id+From+Case+WHERE+callSID__c='1'`,
            headers: {
                'Authorization': 'Bearer ' + sfAuthReponse.access_token,
                'Content-Type': 'application/json',
            },
            body: event.fields,
            json:true,
            method: 'GET'
        };
    
        request(options, processResponse);
  //  }
}

function validateRequest(event) {
    let valid = false;
    let validationMessage;
    
    if(!event.objectAPIName || event.objectAPIName.trim().length === 0) {
        validationMessage = "Parameter, objectAPIName, was not set in the JSON Request Body. Provide the SF API Name of the object to create";
    } else if (!event.fields) {
        validationMessage = "Parameter, fields, was not set in the JSON Request Body. Provide this parameter with a JSON value representing the fields to set when creating the SF object.";
    } else {
        valid = true;
    }
    
    if(!valid) {
        globalCallback(validationMessage);
    }
    
    return valid; // <== This will always return true since execution is terminated with the callback if invalid
}

function processResponse(error, response, body) {
    if (!error && response.statusCode == 200) {
        console.log('response.body.records[0].Id');
        console.log(response.body.records[0].Id);

        // Successfully created new object. Response 201 from successful object creation
        //globalCallback(null, response.body.records[0].Id);
        globalCallback(null, response);
    } else{
        console.log("Error: ", error);
        console.log("Response: ", response);
        console.log(body);
        globalCallback(body);
    }
}

这里是第一个调用上述函数的函数的一些代码,不确定如何在响应中添加点符号。

fetch('https://casdflk-dsfdsfat-8707.twil.io/sf-get-record', {
                        headers: {
                           'Authorization': 'Basic ' + context.ENCODED_TWILIO_CREDS,
                           'Content-Type': 'application/json'
                        },
                        method: 'POST',
                        body: {
                            objectAPIName: 'Case',
                            callSID: '1',
                        }
                    
                 // callback(null,sid);
              }).then(record => {
                  
                    
                    console.log('recordbody11');
                    console.log(record.body);
                    return record;

您似乎在使用 fetch

Fetch 有一个方法.json(),您需要在响应中调用该方法。

MDN page

这是你的问题还是我漏掉了什么?