CB is not a function
Emanuelboi opened this issue · 6 comments
Hi all,
I'm running into an error when I tried to run the script.
` cb(null, _.keyBy(response.accounts, 'number'));
^
TypeError: cb is not a function`
I'm a newbie with Node.JS, is there something I didn't do well?
Thanks! :)
I guess the first thing to ask is, are you passing a function as a callback after your call to the API returns?
To be quite frank, I'm not a Javascript developper, but I'm willing to learn!
I was under the impression that this code was working out of the box, once Node.js was installed. Should I need to adjust some of the code to make it work, if so, do you know where I should start or which documentation I should read to be able to make it work?
Thanks!
It really depends what you are trying to achieve/do. This package wraps the Questtrade oAuth/REST API with functionality for Node. There's nothing stopping you from using the API directly, also. That's what I ended up doing. If you are not familiar with callbacks and/or JS promises, I would start there.
I'm reasonably good at Javascript, and I tried this and ran into problems:
var Questrade = require('questrade');
var qt = new Questrade('./api-key.txt');
qt.on('ready', function () {
// Get Market quotes
qt.getQuote('MSFT');
})
The first issue I discovered was that it did a qt.getAccounts()
without me explicitly calling it. So, I changed my Questrade user app to allow "Retrieve balances, positions, orders and executions". That solved the first issue.
The second issue, when calling qt.getQuote('MSFT')
, is that Questrade responds back with "Requesting anything other than 'application/json'", and since no quote data is returned, it throws that cb error again!
So, I'm still figuring this out, and I'll let you know what I find.
ok, I managed to get it to work.
-
A callback function
cb
is required for almost all the API calls.
So, don't callqt.getQuote(...)
,
instead callqt.getQuote(..., function(err,response) { .... } );
. -
qt.getQuote('MSFT')
will not work. You have to useqt.getSymbol('MSFT',...)
first to get the internal numeric id, and then use that to getQuote.
So, try this code:
var Questrade = require('questrade');
var qt = new Questrade('./.my-questrade-api-key'); //Location of a text file with the API key
qt.on('ready', function () {
qt.getSymbol('MSFT', function(err,response) {
console.log("getSymbol(MSFT) response:", JSON.stringify(response));
let symbolId = response.symbolId;
qt.getQuote( symbolId, function(err,response) {
console.log(`getQuote(${symbolId}) response:`, JSON.stringify(response));
});
});
});
Thanks! I try it out, it didn't fully work for me, but I managed to make it work to retrieved my balances (that was my main goal).
This is the code that works for me:
`var Questrade = require('questrade');
var qt = new Questrade('./.my-questrade-api-key'); //Location of a text file with the API key
qt.on('ready', function () {
qt.getBalances(function (err, balances) {
console.log("getBalances() response:", JSON.stringify(balances));
});
});`