I have module which get cookie, but didnt know how to use it in other js file

问题内容:

Here is code:

var Nightmare = require('nightmare');
var nightmare = Nightmare()

var startURL = 'http://somelink';

module.exports = function(email, password, work){
nightmare
 .goto(startURL)
   .type('input[id="ctl00_cphMainContent_Login1_UserName"]', email)
   .type('input[id="ctl00_cphMainContent_Login1_Password"]', password)
   .click('#ctl00_cphMainContent_Login1_LoginButton')
   .wait()
   .cookies.get('ASP.NET_SessionId')
   .end()
   .then(function (cookies) {
     work(cookies);
   })
   .catch(function (error) {
     console.error('Authorization failed:', error);
   });
}

I was read about module.export but didnt get how my code should work with work variable, now its get object and i need to use value from it in other file like variable:

{ name: 'ASP.NET_SessionId',
  value: 'pmxqwbvzwbgk0praeirviwqy',
  domain: 'link',
  hostOnly: true,
  path: '/',
  secure: false,
  httpOnly: true,
  session: true }

Any thoughs about it? I need get value from cookie object, and use it as variable in request function.

问题评论:

    
Since the cookie is something you retrieve when a function is called in moduleA, you probably need to return the cookie from that function so the caller can get access to it. Now, since your function is asynchronous, you probably need to return a promise and make the cookies part of the resolved value of the promise. Then, you can get access to the cookies from the caller. If you want further help, then show us the code you want to use it in and how that code calls this module.

原文地址:

https://stackoverflow.com/questions/47754662/i-have-module-which-get-cookie-but-didnt-know-how-to-use-it-in-other-js-file

添加评论

友情链接:蝴蝶教程