简体   繁体   English

使用 request-promise-native 实现 Dialogflow:无法解析响应

[英]Dialogflow fulfilment using request-promise-native : unable to parse response

I'm performing a REST API call in Salesforce from DialogFlow fulfilment but I just can not read the JSON response.我正在 Salesforce 中从 DialogFlow 执行中执行 REST API 调用,但我无法读取 JSON 响应。 I'd like to access some fields of the JSON response esp CaseNumber.我想访问 JSON 响应 esp CaseNumber 的某些字段。 The query can return 0 or several records.查询可以返回 0 条或多条记录。

Could someone help me just to parse the returned response?有人可以帮我解析返回的响应吗? Thanks!谢谢!

  function ticket(agent) {
    agent.add('Looking for ticket --> ' + agent.parameters.numero);
    var rpTicket = require('request-promise-native');   
    var optionsTicket = {
    method: 'GET',
    uri: "https://eu26.salesforce.com/services/data/v44.0/query?q=select CaseNumber from Case where ContactID='XXXXXXX'+order+by+CreatedDate+desc",   
    headers: {
        'Accept': 'application/json',
        'Authorization': 'Bearer XXXXXXXXXX'
     },
    json: true
    };

    return rpTicket( optionsTicket )
    .then( body => {
        console.log( 'Ok here is the JSON --> ' + util.inspect(body,false,null)); // I can read the JSON response in the Google Log console so I m fine
        var myTickets = JSON.parse(util.inspect(body,false,null)); 
        // or
        //var myTickets = JSON.parse(body); 
        console.log( 'Ok here is the # of tickets --> ' + myTickets.count); // Throws an error
        return Promise.resolve( true );
    })
    .catch( err => {
        // You should also return a message here of some sort
        console.log( 'Error ... --> ' + err);
        return Promise.resolve( true );
    });     
  }

Since you have json: true in your request-promise configuration, the body that is returned is already a JavaScript object.由于您的 request-promise 配置中有json: true ,因此返回的body已经是一个 JavaScript 对象。 So you do not need to call JSON.parse(body) .所以你不需要调用JSON.parse(body)

You should just be able to read the value you want with body.count (assuming count is a field that is returned).您应该能够使用body.count读取您想要的值(假设count是一个返回的字段)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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