【问题标题】:Express.js modules & Parse.com Cloud CodeExpress.js 模块和 Parse.com 云代码
【发布时间】:2014-06-20 00:02:48
【问题描述】:

我正在使用 Parse.com 和 express.js。我是 webdev 的新手,Parse.com 让事情变得复杂。

我想使用外部 SDK - QuickBlox。 SDK的结构如下:

当我将quickblox.js 包含在<script> 标记中,然后调用QB.init() 时,SKD 工作正常。

但是,我想将所有这些功能放在我的一条路线中。问题是,我不能npm install,因为我使用的是 Parse 的云服务器。

所以我将quickblox.js 复制到云文件夹中,并尝试将其作为模块加载:

var QB = require('cloud/libs/quickblox.js');

我对@9​​87654329@ 做了同样的事情,它奏效了。但是,它不适用于 Quickblox。当我打电话给QB.init() 现在我得到:

TypeError: Object [object Object] has no method 'init'

问题是,我是这个环境的新手,所以我无法判断这是我做错了什么基本的事情还是特殊情况。


编辑

我查看了quickblox.js 并发现了这些可能相关的行:

// Creating a window scoped QB instance
if (typeof window !== 'undefined' && typeof window.QB === 'undefined') {
  window.QB = new QuickBlox();
}

// Browserify exports
module.exports = (typeof window === 'undefined') ? new QuickBlox() : QuickBlox;

所以,因为我没有运行这个客户端,所以没有window 对象和window.QB = new QuickBlox(); 永远不会运行,对吧?这是 .js 文件中对 QB 的唯一引用。

既然(typeof window === 'undefined') 应该为真,那么module.exports 被设置为new QuickBlox()...但是我要引用什么对象来获得init()

这里是 init() 的定义:

  // Actual QuickBlox API starts here
  function QuickBlox() {}

  QuickBlox.prototype.init = function(appId, authKey, authSecret, debug) {
  this.service = new Proxy();
  this.auth = new Auth(this.service);
  this.users = new Users(this.service);
  this.content = new Content(this.service);
  this.location = new Location(this.service);
  this.messages = new Messages(this.service);
  this.data = new Data(this.service);

  // Initialization by outside token
  if (typeof appId === 'string' && !authKey && !authSecret) {
    this.service.setSession({ token: appId });
    appId = '';
  }

  config.creds.appId = appId;
  config.creds.authKey = authKey;
  config.creds.authSecret = authSecret;
  if (debug) {
    config.debug = debug;
    console.log('QuickBlox.init', this);
  }
};

And the actual quickblox.js file on github.

【问题讨论】:

  • 你必须学习如何添加 quickblox 服务器端库我也有同样的问题。并尝试寻找方法。
  • 您可以使用 quickblox api 做到这一点。但在这种情况下,您需要添加一些客户端 api 请求。

标签: express parse-platform quickblox


【解决方案1】:

从 QuickBlox github 的简要介绍来看,您似乎会将“js”文件夹的内容放在“cloud”文件夹或“QuickBlox”子文件夹中,您不会使用浏览器版本除非您想从客户端浏览器访问 QuickBlox。

假设您将所有内容都放在名为“QuickBlox”的子文件夹内的“js”文件夹中,然后您将通过添加 requires(..) 语句在 Cloud Functions 中引用它,例如

var QB = requires('QuickBlox/quickblox');

这将加载所有模块并将它们也附加到 QB 变量,因此在您的 Cloud Functions 中,您可以执行以下操作(从他们网站上的示例中提取):

QB.init(appId, authKey, authSecret);

// create an API session (user is not authenticated)
QB.createSession(function(err, result) {
  if (err) { 
    console.log('Something went wrong: ' + err);
  } else {
    console.log('Session created with id ' + result.id);
  }
});

// list the users currently enrolled
QB.users.listUsers(function(err, result) {
  for (var i=0; i < result.items.length; i++) {
    console.log('User ' + result.items[i].login + ' is registered');
  }
});

【讨论】:

  • 它不起作用,因为这种库工作客户端而不是服务器端
猜你喜欢
  • 2015-08-08
  • 1970-01-01
  • 2016-05-04
  • 1970-01-01
  • 2014-08-31
  • 2014-11-23
  • 2016-02-08
  • 2016-02-24
  • 2013-06-23
相关资源
最近更新 更多