简体   繁体   English

Node.js函数将字符串返回到主程序

[英]Nodejs function to return string to main program

I am having an issue with turning nodejs code into a function that returns a string so I can use that string later in the code. 我在将nodejs代码转换为返回字符串的函数时遇到问题,因此稍后可以在代码中使用该字符串。

When I turn this into a function, I dont have anything returning. 当我将其转换为函数时,我什么也没有返回。

I'm not sure if it is a callback issue, because I would think I should have a return of undefined. 我不确定这是否是回调问题,因为我认为应该返回undefined。

Not sure what the issue is. 不知道是什么问题。

 var request = require('request-promise');

    let authtoken;  

function remedyLogin() {
var uri = 'https://test-XXX.com';

var _include_headers = function(body, response, resolveWithFullResponse) {
  return {'headers': response.headers, 'data': body};
};

var options = {
  method: 'POST',
  uri: uri,
  json: true,
  transform: _include_headers,
  body: { "username":"XXXXXXX", "password":"XXXXXXXXXXXXXx"
    },
}

request(options)
    .then(function (parsedBody) {
        console.log("post success");
    })
    .catch(function (err) {
        console.log("post failed");
    });

return request(options)
.then(function(response) {

var result = JSON.stringify(response.headers);
var res = result.match(/{\"authentication-token":".*","content-type/);
stringgy = res[0].substring(25);
authtoken = stringgy.substring(0, stringgy.length-17);
console.log(authtoken);
return authtoken;

});

}

var foo = RemedyLogin;
console.log(foo);

This code below works fine and I'm returned an auth code as follows. 下面的代码工作正常,我返回了一个身份验证代码,如下所示。

post success
/DwG7gAAAAAAAAAA4RZPA8x0Ny3ztp0ImAAk8M0bCLDkUzwA0HNG8CSAxtws3QQu353Gdb3/stXd0o
PMgh7Wgaq99rUOa1whKC0TOfFCtD8c9SZHWlyX9Do7CxdffOeGtMphw+EiC8R/JEnPxJJUq/g+eUlg
bDcB2S0w2w2XGNlJ/3HNM4rIpbn+me9EK9xNMkI2F+WrzkWMnQaUWQn/VTi6l84eQR1CuPdDrn6r4t
MADTFlWYpFL7maqqYAmEzYBahIQ+uRwautvBVwyQJSstV8xTbVso2bhe7VI+ms/bI0BR4yVaJQzg/9
rAxj2QbAWsRowT3p22y3BmWzkXpgGT2QcU6SKr2/PyuWf1Lo9xMTtgZujofA+iuPwOv0zAHL9wg5qY
AbcQYS9RKSj1FltMIMgsvKVJlXhIKAQLih4kVaPklMhF/x4qYg5q8GuDbm0IyicrxJFPISpe2ba8GI
MqurgGUsdiaqNrRSXjevbuyeuJ7nA+TLSGUG0hYxg6OT9CNVfJ6It8hWPvD/OtFhAncVgA

Here is the code. 这是代码。

 var request = require('request-promise');
    var uri = 'https://test-orchestrator.lmig.com/baocdp/rest/login';


    var _include_headers = function(body, response, resolveWithFullResponse) {
      return {'headers': response.headers, 'data': body};
    };

var options = {
  method: 'POST',
  uri: uri,
  json: true,
  transform: _include_headers,
  body: { "username":"sahst_trap_parser_ao", "password":"cgSW68eJexsPet"
    },
}

request(options)
    .then(function (parsedBody) {
        console.log("post success");
    })
    .catch(function (err) {
        console.log("post failed");
    });

return request(options)
.then(function(response) {

var result = JSON.stringify(response.headers);
var res = result.match(/{\"authentication-token":".*","content-type/);
stringgy = res[0].substring(25);
var authtoken = stringgy.substring(0, stringgy.length-17);
console.log(authtoken);
});

Looks like you called the remedyLogin function incorrectly. 看起来您错误地调用了remedyLogin函数。 Try to change this code: 尝试更改此代码:

var foo = RemedyLogin;
console.log(foo);

to

remedyLogin().then(foo => console.log(foo));

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

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