简体   繁体   English

如何使用RequireJS for Yelp API,客户端JavaScript

[英]How to use RequireJS for Yelp API, clientside javascript

I am trying to incorporate the yelp api into my web application. 我正在尝试将yelp api合并到我的Web应用程序中。 I would like the user to be able to make requests to the yelp servers without going through my server and adding additional data transmission. 我希望用户能够不通过我的服务器并添加其他数据传输而向yelp服务器发出请求。

How do I require yelp and also call yelp.CreateClient with requireJS? 我如何要求yelp并同时使用requireJS调用yelp.CreateClient? Or am I on the wrong track? 还是我走错了路?

The setup: -lib 设置:-lib

-node_modules -node_modules

-public--javascripts---fileusingyelp.js -public - Java脚本--- fileusingyelp.js

-public--javascripts---yelp library -public--javascripts --- yelp库

-routes -routes

-views -views

app.ks app.ks

package.json 的package.json


I am following this demo . 我正在关注这个演示 The code: 编码:

var yelp = require("../index").createClient({
  consumer_key: process.env.CONSUMER_KEY,
  consumer_secret: process.env.CONSUMER_SECRET,
  token: process.env.TOKEN,
  token_secret: process.env.TOKEN_SECRET,
  ssl: true
});


yelp.search({term: "food", location: "Montreal"}, function(error, data) {
  console.log(error);
  console.log(data);
});

As I understand it, I can not use require in the normal way on the clientside. 据我了解,我不能在客户端以常规方式使用require。 Because of this, I need to use the RequireJS library. 因此,我需要使用RequireJS库。

Here is my code: 这是我的代码:

var yelp = require(["javascripts/yelp/index"] , function(inp){
  yelp.createClient({//second error
  consumer_key: "key",
  consumer_secret: "secret",
  token: "token",
  token_secret: "token_secret",
  ssl: true
});
});


var yresponse = yelp.search({term: "food", location: "Montreal"}, function(error, data) {//first error is here
  console.log(error);
  console.log(data);
});

Here are the errors: Uncaught TypeError: undefined is not a function coursecreation.js:12 错误如下:未捕获的TypeError:undefined不是一个函数coursecreation.js:12

Uncaught ReferenceError: module is not defined index.js:1 未捕获的ReferenceError:模块未定义index.js:1

//this reference error is about the index.js file in the yelp library, //此参考错误与yelp库中的index.js文件有关,

module.exports = require('./lib/yelp');

Uncaught TypeError: undefined is not a function coursecreation.js:2 Uncaught TypeError:undefined不是一个函数coursecreation.js:2

I've taken a look at the yelp library you are trying to use. 我看过您要使用的yelp库 It is made for use with Node.js. 它是与Node.js一起使用的。 In general you can't just take a library created for use with Node.js and load it with RequireJS. 通常,您不能只使用创建 Node.js使用的库并使用RequireJS加载它。 In the best case scenario, you can convert a code-base made for Node.js into something that RequireJS uses by wrapping the code with define calls. 在最佳情况下,您可以通过使用define调用包装代码,将为Node.js创建的代码库转换为RequireJS使用的代码库。

Unfortunately, the best case scenario does not apply here because node-yelp actually uses some libraries specific to Node.js (for instance, querystring ) and it calls on node-oauth which itself uses libraries specific to Node.js (for instance, crypto ). 不幸的是,最佳情况不适用于此处,因为node-yelp实际上使用了一些特定于Node.js的库(例如querystring ),并且它调用了node-oauth本身使用了特定于Node.js的库(例如crypto )。 To be able to use node-yelp with RequireJS you'd have to modify node-yelp and maybe node-oauth to be able to operate without the libraries that are specific to Node.js. 为了能够将node-yelp与RequireJS一起使用,您必须修改node-yelp,甚至可能修改node-oauth,以使其能够在没有特定于Node.js的库的情况下运行。 This is a major undertaking. 这是一项重大任务。

Note here that it is possible to design a library for use with RequireJS and use in Node.js. 请注意, 可以设计一个与RequireJS一起使用并在Node.js中使用的库。 (I've done it.) It's just that node-yelp was not designed this way. (我已经完成了。)只是node-yelp并不是这样设计的。

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

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