当绑定导出的函数时,React:“Undefined”

时间:2017-04-05 13:43:16

标签: javascript function reactjs

让我先说一下我是React和JS的新手,到目前为止,我读过的文档并没有帮助我解决这个问题,而且我似乎遇到了问题。

我有两个js文件,一个是login.js,另一个是menu.js.我正在尝试将令牌(从login.js)输出到由OnClick函数触发的控制台(在menu.js中)当我点击时我收到一个未定义的错误。最终目标是让我能够访问我从login.js获得的令牌,并在其他文件和组件中使用它,这样我就可以在登录后保留身份验证。 我错过了什么?

由于

**//login.js snippet**

var jsforce = require('jsforce');
var conn = new jsforce.Connection({
  oauth2 : {
    // you can change loginUrl to connect to sandbox or prerelease env.
    loginUrl : 'https://test.salesforce.com',
     clientId : '0123456789',
    clientSecret : '0123456789',
    redirectUri : 'https://test.salesforce.com/services/oauth2/token',
  }
});
conn.login(email, password, function(err, userInfo) {
  if (err) { return console.error(err,email,password);
 }
  // Now you can get the access token and instance URL information.
  // Save them to establish connection next time.
  //console.log(conn.accessToken);
  console.log(conn.instanceUrl);
  exports.token = conn.accessToken


  // logged in user property
  console.log("User ID: " + userInfo.id);
  console.log("Org ID: " + userInfo.organizationId);
  // ...
    //Forward na Profil screen
    setTimeout(function(){
      forwardTo('/menu');
    },1280);  


})





**//Menu.js snippet**

import React from 'react';
import ReactDOM from 'react-dom';
import { Link } from 'react-router';
import { render } from 'react-dom';
import login from '../pages/login.js';
var jsforce = require('jsforce');


onLinkClicked () {

    console.log(login.token)
  }


  render () {

    return (
        <div className='menubox' id='menubox'>

     <button
            type='submit'
            onClick={this.onLinkClicked.bind(this)}>
            Search
          </button>
      </div>
    )
  }



}



export default menuScreen;

2 个答案:

答案 0 :(得分:0)

点击后,您确定登录响应是有效还是加载?您可以在chrome中使用开发人员工具并检查响应是什么。

答案 1 :(得分:0)

我能够使用sessionStorage.setItem保存令牌(&#34;令牌&#34;,令牌);在login.js中,通过将accessToken:sessionStorage.getItem(&#39; token&#39;)添加到onClick函数,从menu.js中检索令牌。我想sessionstorage是你想要全局访问身份验证数据的方法。我能够成功查询该站点。谢谢你的帮助!

**//login.js snippet**

var jsforce = require('jsforce');
var conn = new jsforce.Connection({
  oauth2 : {
    // you can change loginUrl to connect to sandbox or prerelease env.
    loginUrl : 'https://test.salesforce.com',
     clientId : '0123456789',
    clientSecret : '0123456789',
    redirectUri : 'https://test.salesforce.com/services/oauth2/token',
  }
});
conn.login(email, password, function(err, userInfo) {
  if (err) { return console.error(err,email,password);
 }

  var token = conn.accessToken;

  console.log(conn.instanceUrl);
  exports.token = conn.accessToken


  sessionStorage.setItem("token", token);


  // logged in user property
  console.log("User ID: " + userInfo.id);
  console.log("Org ID: " + userInfo.organizationId);
  // ...
    //Forward na Profil screen
    setTimeout(function(){
      forwardTo('/menu');
    },1280);  


})





**//Menu.js snippet**

import React from 'react';
import ReactDOM from 'react-dom';
import { Link } from 'react-router';
import { render } from 'react-dom';
import login from '../pages/login.js';
var jsforce = require('jsforce');





onLinkClicked () {
   var jsforce = require('jsforce');



var jsforce = require('jsforce');

var conn = new jsforce.Connection({
  serverUrl : 'https://cs63.salesforce.com',
  accessToken : sessionStorage.getItem('token')
 })
 conn.search("FIND {Un*} IN ALL FIELDS RETURNING Account(Id, Name), Lead(Id, Name)",
  function(err, res) {
    if (err) { return console.error(err); }
    console.log(res);
  }
)
}

      render () {

        return (
            <div className='menubox' id='menubox'>

         <button
                type='submit'
                onClick={this.onLinkClicked.bind(this)}>
                Search
              </button>
          </div>
        )
      }



}



export default menuScreen;