Question

I am trying to incorporate the yelp api into my web application. 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.

How do I require yelp and also call yelp.CreateClient with requireJS? Or am I on the wrong track?

The setup: -lib

-node_modules

-public--javascripts---fileusingyelp.js

-public--javascripts---yelp library

-routes

-views

app.ks

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. Because of this, I need to use the RequireJS library.

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

Uncaught ReferenceError: module is not defined index.js:1

//this reference error is about the index.js file in the yelp library,

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

Uncaught TypeError: undefined is not a function coursecreation.js:2

Was it helpful?

Solution

I've taken a look at the yelp library you are trying to use. It is made for use with Node.js. In general you can't just take a library created for use with Node.js and load it with 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.

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). 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. This is a major undertaking.

Note here that it is possible to design a library for use with RequireJS and use in Node.js. (I've done it.) It's just that node-yelp was not designed this way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top