Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将facebook响应从一个js文件传输到另一个js文件的最佳方式_Javascript_Node.js_Facebook_Google Maps - Fatal编程技术网

Javascript 将facebook响应从一个js文件传输到另一个js文件的最佳方式

Javascript 将facebook响应从一个js文件传输到另一个js文件的最佳方式,javascript,node.js,facebook,google-maps,Javascript,Node.js,Facebook,Google Maps,我想知道从Facebook验证用户后得到的响应对象从一个javascript文件传输到另一个javascript文件的最佳方式 我想这样做,因为我想获得用户的朋友位置,它在我的fb.js文件中,并将其传输到gmap.js文件中,在那里我将创建地图并添加标记 我使用nodeJS作为我的服务器,我使用module.exports将数据从一个文件传输到另一个文件。但在这种情况下,我真的不知道该如何导出它。任何转移反应的方法都对我有好处 function login() { FB.login(

我想知道从Facebook验证用户后得到的响应对象从一个javascript文件传输到另一个javascript文件的最佳方式

我想这样做,因为我想获得用户的朋友位置,它在我的
fb.js
文件中,并将其传输到
gmap.js
文件中,在那里我将创建地图并添加标记

我使用nodeJS作为我的服务器,我使用
module.exports
将数据从一个文件传输到另一个文件。但在这种情况下,我真的不知道该如何导出它。任何转移反应的方法都对我有好处

function login()
{   
  FB.login(function (response) 
  {
      if(response.authResponse)
      {  
          console.log("login successful");
      }
  },{scope: "public_profile,email,user_tagged_places,user_friends" });
}

由于您似乎没有听取我和其他人的建议,所以这里的代码将把您的对象“传输”到另一个javscript文件

同样,这绝对不是正确的方法。据我所知,你的问题是:你给Facebook(或任何地方)打了一个ajax电话,然后得到回复。您现在需要获取这个响应对象,并将其传递给另一个javascript文件中的代码,该文件此时不可访问。这里要做的正确的事情是使jaascript文件——或者是整个文件,或者只是需要访问的函数。在面向对象的语言中,您可能会更改权限级别以使这些函数
公开
,但在这里显然无法做到这一点,因此您有几个选项:

-使用require.js执行类似操作

// this will be at the top of your javascript file.
// what this is doing is pulling your fb.js file into this file
// and making ever function in fb.js accessible here
define(['javascript/myPathToJsFiles/gmap'],
  function( gmap) {
  // everything in this js file will go in here
  // all of this code has fb and gmap available to use with 'fb' and 'gmap'


  function login()
  {   
      FB.login(function (response) 
      {
          if(response.authResponse)
          {  
              var respFromGmap =  gmap.myFunctionNameInGmapFile(response);
              console.log("login successful");
          }
      },{scope: "public_profile,email,user_tagged_places,user_friends" });
    }
..
..
好的,这里有一段代码,如果您只想忽略所有内容,并将一个对象传输到另一个文件,并使它们彼此完全分离。我的示例是调用
FB.Login
,然后存储对全局变量的响应,然后可以在任何地方访问全局变量

FB.login(function (response) 
{
    if(response.authResponse)
    {  
        // if this code is running on a browser replace  GLOBAL with window

        // I can't stress enough how bad of an idea this is , but anyway if
        // you must  use a global var then make sure it has a really long 
        // and really descriptive name to prevent getting mixed up and 
        // overridden
        GLOBAL.facebookLoginResponseObject = response;
    }
}
现在,在gmap.js或任何其他js文件中,由于孤独和反社会,无法通过
FB.login访问该代码

fuction useLoginObject(){
   // the "|| {}" just makes sure you won't throw an exception , it will return 
   // an empty object if nothing was found in the global 
   var fbLoginObject = GLOBAL.facebookLoginResponseObject || {};
}

由于您似乎没有听取我和其他人的建议,所以这里的代码将把您的对象“传输”到另一个javscript文件

同样,这绝对不是正确的方法。据我所知,你的问题是:你给Facebook(或任何地方)打了一个ajax电话,然后得到回复。您现在需要获取这个响应对象,并将其传递给另一个javascript文件中的代码,该文件此时不可访问。这里要做的正确的事情是使jaascript文件——或者是整个文件,或者只是需要访问的函数。在面向对象的语言中,您可能会更改权限级别以使这些函数
公开
,但在这里显然无法做到这一点,因此您有几个选项:

-使用require.js执行类似操作

// this will be at the top of your javascript file.
// what this is doing is pulling your fb.js file into this file
// and making ever function in fb.js accessible here
define(['javascript/myPathToJsFiles/gmap'],
  function( gmap) {
  // everything in this js file will go in here
  // all of this code has fb and gmap available to use with 'fb' and 'gmap'


  function login()
  {   
      FB.login(function (response) 
      {
          if(response.authResponse)
          {  
              var respFromGmap =  gmap.myFunctionNameInGmapFile(response);
              console.log("login successful");
          }
      },{scope: "public_profile,email,user_tagged_places,user_friends" });
    }
..
..
好的,这里有一段代码,如果您只想忽略所有内容,并将一个对象传输到另一个文件,并使它们彼此完全分离。我的示例是调用
FB.Login
,然后存储对全局变量的响应,然后可以在任何地方访问全局变量

FB.login(function (response) 
{
    if(response.authResponse)
    {  
        // if this code is running on a browser replace  GLOBAL with window

        // I can't stress enough how bad of an idea this is , but anyway if
        // you must  use a global var then make sure it has a really long 
        // and really descriptive name to prevent getting mixed up and 
        // overridden
        GLOBAL.facebookLoginResponseObject = response;
    }
}
现在,在gmap.js或任何其他js文件中,由于孤独和反社会,无法通过
FB.login访问该代码

fuction useLoginObject(){
   // the "|| {}" just makes sure you won't throw an exception , it will return 
   // an empty object if nothing was found in the global 
   var fbLoginObject = GLOBAL.facebookLoginResponseObject || {};
}

我不太清楚为什么需要将一个对象“传输”到另一个文件?您是否有要访问另一个文件中的函数的权限?我建议使用require.js,只需拉入其他js文件,您就可以访问所有文件中的所有函数。。。不要将对象“传输”到另一个文件我不想访问我的facebook功能。但是,他们给我的回复中包含了姓名和位置。这是我的谷歌地图添加标记所需要的。那么你是说使用require.js会得到facebook返回到我的google map js文件的响应?您能告诉我如何使用require.js专门传输响应吗。我添加了我在上面得到的回复@斯科特塞尔比,你不能“转移”东西。你编写的代码调用FBAPI,获取数据,并告诉GMAP通过他们的API做一些事情。这就是所谓的编程。他们的JavaScript文件不知道如何处理你的对象,除非你创建一个程序。我真的不知道为什么你需要将一个对象“传输”到另一个文件?您是否有要访问另一个文件中的函数的权限?我建议使用require.js,只需拉入其他js文件,您就可以访问所有文件中的所有函数。。。不要将对象“传输”到另一个文件我不想访问我的facebook功能。但是,他们给我的回复中包含了姓名和位置。这是我的谷歌地图添加标记所需要的。那么你是说使用require.js会得到facebook返回到我的google map js文件的响应?您能告诉我如何使用require.js专门传输响应吗。我添加了我在上面得到的回复@斯科特塞尔比,你不能“转移”东西。你编写的代码调用FBAPI,获取数据,并告诉GMAP通过他们的API做一些事情。这就是所谓的编程。他们的JavaScript文件不知道如何处理你的对象,除非你创建一个程序。这些建议对我来说更像是一种打击。请原谅我的“转移”术语,因为我对编程非常陌生,这是我糟糕的@ScottSelby。不过,非常感谢您提供了急需的答案。我试过了你写的所有技巧。将我所有的facebook和google地图功能放在一个文件中对我来说很有用。我想问@ScottSelby的另一件事是,我可以在app.js和routes.js文件中使用require和其他模块。但是,我不能在公用文件夹(即客户端文件夹)中的js文件中使用require.js、module.exports或GLOBAL关键字。每次启动我的服务器并加载链接到客户端JS文件的页面时,我都会遇到引用错误,例如,requirenotdefine