kevinohara80 / nforce

nforce is a node.js salesforce REST API wrapper for force.com, database.com, and salesforce.com
MIT License
474 stars 167 forks source link

Cannot read property 'instance_url' of undefined #114

Closed bemau closed 8 years ago

bemau commented 8 years ago

Folks, any thought?

$ node force.js 
/Users/clientX/node_modules/nforce/index.js:905
      opts.oauth.instance_url,
                ^

TypeError: Cannot read property 'instance_url' of undefined
    at Connection._apiRequest (/Users/clientX/node_modules/nforce/index.js:905:17)
    at Connection._queryHandler (/Users/clientX/node_modules/nforce/index.js:674:8)
    at Connection.query (/Users/clientX/node_modules/nforce/index.js:618:15)
    at Object.<anonymous> (/Users/clientX/Documents/Nodejs/force.js:23:5)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:139:18)
    at node.js:968:3

here the code

var nforce = require('nforce');

var org = nforce.createConnection({
  clientId: 'xxxxx',
  clientSecret: 'xxxxxx',
  redirectUri: 'http://localhost:3000/oauth/_callback',
  apiVersion: 'v36.0',  // optional, defaults to current salesforce API version
  environment: 'production',  // optional, salesforce 'sandbox' or 'production', production default
  mode: 'single' // optional, 'single' or 'multi' user mode, multi default
});
var oauth;
org.authenticate({ username: 'xxxxxx', password: 'xxxxxxx'}, function(err, resp){
  // store the oauth object for this user
  if(!err){ 
    oauth = resp;
    console.log('Cached Token: ' + org.message);
    } else {
        console.log('Error: ' + err.message);
    }
});

org.query({ query: 'SELECT Id FROM Lead LIMIT 1' }, function(err, res) {
  if(err) return console.error(err);
  else return console.log(res.records[0]);
});
kevinohara80 commented 8 years ago

Like all API calls, authenticate() is asynchronous. Therefore that callback needs to happen before you call query(). Try moving your query() code inside of the callback for authenticate().

Also, you don't need to create a variable for oauth if you are using single user mode since the oauth credentials are cached inside of the connection object. You can completely omit that.

bemau commented 8 years ago

thanks @kevinohara80, works perfectly.

anil826 commented 6 years ago

Having same issue with nforce node module for chatter feeds .

TypeError: Cannot read property 'instance_url' of undefined at Connection.getUrl (/home/ubuntu/workspace/node_modules/nforce/index.js:709:24) at get_feed (/home/ubuntu/workspace/components/chatter/middleware.js:70:19) at canvas_authentication (/home/ubuntu/workspace/components/chatter/middleware.js:82:7) at callbacks (/home/ubuntu/workspace/node_modules/express/lib/router/index.js:164:37) at /home/ubuntu/workspace/components/organization/index.js:47:20 at Query.<anonymous> (/home/ubuntu/workspace/node_modules/mongoose/lib/model.js:4020:16) at /home/ubuntu/workspace/node_modules/kareem/index.js:273:21 at /home/ubuntu/workspace/node_modules/kareem/index.js:131:16 at _combinedTickCallback (internal/process/next_tick.js:73:7) at process._tickCallback (internal/process/next_tick.js:104:9)

By calling this way... Salesforce.getUrl('/services/data/v29.0'+OAuth.community+'/chatter/feeds/news/'+requestContext.userId+'/feed-items?pageSize=10', OAuth, function(err,resp){ if(!err) { sf_data = resp; render_response(); }else{ console.log('***error: ' + err); response.write(err); response.end(); } });

bijays commented 6 years ago

Hi Kevin,

Here is my code snippet through which i am trying to call an apex rest service and i get the error if(callback) callback(null, data); ^

TypeError: callback is not a function

`'use strict'

var nforce = require('nforce'); var query = 'SELECT Id, FirstName, LastName, Email FROM Lead LIMIT 10';

var oauth; var org = nforce.createConnection({ clientId: 'dfgdfgdfgfdgfd.2XjQ89MlDtOeYXx3SGF0VlfkRqjnYWRJUzj3rE54VhYWlfreMFlRODJIMlcWQ', clientSecret: 'dfgdfggdfgfd', redirectUri: 'http://localhost:3000/oauth/_callback', apiVersion: 'v34.0', environment: 'production', mode: 'single', autoRefresh: true });

org.authenticate({ username: 'gdfgdfgdfg', password: 'gddfgdf'}, function(err, resp) { if(err) { console.error('unable to authenticate to sfdc'); } else { oauth = resp;

org.query({ query: query, oauth: oauth }, function(err, resp) {
  //if(err) throw err;
  if(resp.records && resp.records.length) {
    resp.records.forEach(function(rec) {
      console.log('Lead: ' + rec.get('FirstName') + ' ' + rec.get('LastName'));
    });
  }
});

org.apexRest({uri:'/BlockList/', method: 'GET', urlParams: [{key:'keyWord', value:'hi'}]}, oauth, function(err, resp) {
  //console.log(resp);
  if(!err) {
    console.log(resp);
    //res.send(resp);
  }else{
    console.log(err);
    //
    //res.send(err);
  }
});

} }); module.exports = org;`

Not sure what exactly is the issue, could you please suggest.

Thanks in adavance.

Regards, Bijay