Error: Response not received using IBMCloud VisualRecognition NodeJS - javascript

I'm follow the getting started's IBMCloud Visual Recognition page: https://cloud.ibm.com/docs/services/visual-recognition?topic=visual-recognition-getting-started-tutorial&programming_language=javascript#getting-started-tutorial.
The follow code is presented:
var VisualRecognitionV3 = require('watson-developer-cloud/visual-recognition/v3');
var fs = require('fs');
var visualRecognition = new VisualRecognitionV3 ({
version: '2018-03-19', iam_apikey: '{apikey}'
});
var url= 'https://watson-developer-cloud.github.io/doc-tutorial-downloads/visual-recognition/640px-IBM_VGA_90X8941_on_PS55.jpg';
var params = {
url: url,
};
visualRecognition.classify (params, function (err, response) {
if (err) {
console.log(err);
} else {
console.log(JSON.stringify(response, null, 2))
}
});
When I execute it via node on Windows I get the follow error message:
error: { Error: Response not received. Body of error is HTTP ClientReque
t
at formatError (C:\Users\augusto\node_modules\ibm-cloud-sdk-c
requestwrapper.js:111:17)
at C:\Users\augusto\node_modules\ibm-cloud-sdk-core\lib\reque
r.js:259:19
at process._tickCallback (internal/process/next_tick.js:68:7)
message:
'Response not received. Body of error is HTTP ClientRequest object',
I've installed alright like the tutorial and I don't know where are the error.

Solved using:
const visualRecognition = new VisualRecognitionV3({
version: '{version}',
iam_apikey: '{apikey}'
disable_ssl_verification: true,
});

Related

How to set path for single page application in Hapi js

I tried this https://github.com/hapijs/hapi/issues/800. This didn't work for me.
const start = async () => {
await server.register(require('inert'));
server.route({
method: 'GET',
path: '/samplespa/{file*}',
handler: function (request, h) {
directory :{
path : './samplespa/'
listing: true
}
}
});
server.ext('onPostHandler', (request, reply) => {
console.log('WORD');
const response = request.response;
if (response.isBoom && (response.output.statusCode === 500) ) {
return reply.file('./samplemap.html');
}
return reply.continue;
});
await server.start();
console.log('Server running at:', server.info.uri);
};
start();
I want to server the directory samplespa and render the file "index.html" in it and note that index.html is written in Angular 1.X and depends on files in the directory..
Also for the path : http://localhost:8000/samplespa/index.html
I get the following response
{
"statusCode": 500,
"error": "Internal Server Error",
"message": "An internal server error occurred"
}
I get the following error msg in vs code:
Debug: internal, implementation, error
Error: handler method did not return a value, a promise, or throw an error
at module.exports.internals.Manager.execute (/Users/pavithran/projects/toilet-tracker/node_modules/hapi/lib/toolkit.js:52:29)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
How to do this??? I have tried everything for the past 2 days and not able to figure it out..
For single file you can use this:
server.route({
method: 'GET',
path: '/samplespa/index.html',
handler: function (request, h) {
return h.file('./samplespa/index.html');
}
});
For multiple files, the following. Note that the handler is now not a function but an object.
server.route({
method: 'GET',
path: '/samplespa/{file*}',
handler: {
directory: {
path: 'samplespa'
}
},
});

Unknown authentication strategy: hapi-auth-bearer-simple

I am trying to use hapi-auth-bearer-simple module to enable bearer token on my app. However, I am getting the error shown in the title.
I am trying to implement this module to enable token authorisation in my app But I am getting error mentioned below
e:\python_training\Training\Node\Test\Project\Backend\node_modules\hapi\node_modules\hoek\lib\index.js:723
I have a route file
module.exports = [
{
method: 'GET',
path: '/api/{_id?}',
handler: function (request, reply) {
Controller.control.get(request.params, function (err, success) {
console.log(request.params);
if (err) {
reply(unifunc.sendError(err));
} else {
reply(unifunc.sendSuccess(SuccessMsg,success)).code(200);
}
});
},
config: {
description: 'desc',
tags: ['api', 'oV'],
validate: {
headers: unifunc.authorizationHeaderObj,
params: {
o_id: Joi.string().required().trim(),
_id: Joi.string().optional().trim()
},
failAction: unifunc.failActionFunction
},
auth: {
strategy: 'bearer',
scope: ['admin', 'user-{params.id}']
},
plugins: {
'hapi-swagger': {
responseMessages: msgs
}](url)
and a controller file in which I mentioned strategy
var bearerSimple= require('hapi-auth-bearer-simple')
authorization = Authorization.auth; // This plugin has the logic to validate the token and return the error in case it fails and I am passing accesstoken as parameter in a function in that file
var getV = function(server, params, callbackRoute){
server.register(
[{
register: bearerSimple
}], function(err){
if(err){
console.log("Failed to log the plugin",err);
throw err;
}
server.auth.strategy('bearer', 'bearerAuth', {
authorization : authorization
});
});
console.log(params);
async.series([
function(cb){}
]}
complete error message is:
Error: Unknown authentication strategy: bearer in path: /api/orders/{order_id}/vehicles/{_id?}
at Object.exports.assert (e:\python_training\Training\Node\Test\Project\Backend\node_modules\hapi\node_modules\hoek\lib\index.js:723:11)
at e:\python_training\Training\Node\Test\Project\Backend\node_modules\hapi\lib\auth.js:152:14
at Array.forEach (native)
at internals.Auth._setupRoute (e:\python_training\Training\Node\Test\Project\Backend\node_modules\hapi\lib\auth.js:149:24)
at new module.exports.internals.Route (e:\python_training\Training\Node\Test\Project\Backend\node_modules\hapi\lib\route.js:142:47)
at internals.Connection._addRoute (e:\python_training\Training\Node\Test\Project\Backend\node_modules\hapi\lib\connection.js:375:17)
at internals.Connection._route (e:\python_training\Training\Node\Test\Project\Backend\node_modules\hapi\lib\connection.js:367:18)
at wrappedRoute [as _route] (e:\python_training\Training\Node\Test\Project\Backend\node_modules\newrelic\lib\instrumentation\hapi.js:222:29)
at internals.Plugin._apply (e:\python_training\Training\Node\Test\Project\Backend\node_modules\hapi\lib\plugin.js:460:14)
at internals.Plugin.route
Is there any way I can resolve this issue?
Edit:
I modified server.js file and removed the strategy from controller file
I placed strategy in server.js
var validationFunction = Authorization.auth;
console.log(validationFunction);
server.register(
[{
register: bearerSimple
}], function(err){
if(err){
console.log("Failed to log the plugin",err);
throw err;
}
server.auth.strategy('bearer', 'bearerAuth', {
validationFunction : validationFunction
});
});
and in Authorization file looks like this
function rauth(accessToken, cb) {
var criteria = {accessToken: accessToken};
var projection = {};
var options = {limit: 1};
Service.AdminService.getadmin(criteria, projection, options, function (err, data) {
if (err) {
cb(err);
} else if (data && data.length > 0 && data[0]._id) {
console.log(data);
console.log(data.length);
adminId = data[0]._id;
cb()
} else {
cb(UniversalFunctions.CONFIG.APP_CONSTANTS.STATUS_MSG.ERROR.INVALID_ACCESS_TOKEN);
}
});
Now I am getting this error:
Error: options.validateFunc must be a valid function in bearerAuthentication scheme
I have been breaking my head over this problem from days. Could anyone suggest what could be the problem here?
The only problem I found was with the parameters of callback function passed in validateFunction but I can't remove the parameters as those parameters are being defined in another function called getadmin. Could anyone suggest a workaround for this?
Solved in this issue https://github.com/Salesflare/hapi-auth-bearer-simple/issues/69.
The problems were a typo and needed to pass more info back on a successful authorization.

Meteor: Accounts._storedLoginToken is not a function

I am using CollectionFS with GridFS to upload images:
Pictures.insert(e.target.files[0], function(err, res) {
if (err) return console.log(err);
this.setState({editing: false});
});
However when uploading I get an error:
Exception in delivering result of invoking '/cfs.pictures.filerecord/insert':
TypeError: Accounts._storedLoginToken is not a function
I've tried calling Accounts._storedLoginToken() in the Meteor shell and it gives the same error.
The error occurs here in the Meteor code:
var authToken = '';
if (typeof Accounts !== "undefined") {
var authObject = {
authToken: Accounts._storedLoginToken() || '',
};
// Set the authToken
var authString = JSON.stringify(authObject);
authToken = FS.Utility.btoa(authString);
}
Any help would be much appreciated.
Did you add accounts-base to your project? Accounts object does not have the _storedLoginToken method until you add a certain pkg such as account-base.

Node.js gives error: Can't set headers after they are sent

I am new to node.js and I am doing functionality of fetching all email address from Gmail. After getting responses I am sending responses into jade template. After getting single response I am getting en error in terminal:
Error: Can't set headers after they are sent.
My code is as below:
module.exports.getmailist = function(req , res) {
var google = require('googleapis');
var gmail = google.gmail('v1');
var email_list = Array();
var myObject = Array();
var accounting = [];
var messageIds = [];
var jsonString = false;
var employees = {};
var OAuth2 = google.auth.OAuth2;
var oauth2Client = new OAuth2("--MY CLIENT ID--","SECRET ID","REDIRECT URL");
oauth2Client.credentials = { access_token: req.session.access_token};
var addData = function (req,response) {
console.log(response);
res.render('getmailist',{title: 'Product Template', result: response});
}
gmail.users.messages.list({ userId: 'me', 'q': "inbox", auth: oauth2Client}, function(err, response) {
for(var i = 0; i< response.messages.length; i++) {
//console.log(response.messages[i].id);
gmail.users.messages.get({ userId: 'me', id:response.messages[i].id , auth: oauth2Client}, function(error , resp) {
if(resp != "")
{
addData(req,resp)
}
});
}
return true;
});
};
In console.log(response), I get first datavalue of email as obejct and then below error is displayed.
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (http.js:690:11)
at ServerResponse.header (/var/www/nodegoogle/node_modules/express/lib/response.js:700:10)
at ServerResponse.send (/var/www/nodegoogle/node_modules/express/lib/response.js:154:12)
at fn (/var/www/nodegoogle/node_modules/express/lib/response.js:934:10)
at View.exports.renderFile [as engine] (/var/www/nodegoogle/node_modules/jade/lib/index.js:374:12)
at View.render (/var/www/nodegoogle/node_modules/express/lib/view.js:93:8)
at EventEmitter.app.render (/var/www/nodegoogle/node_modules/express/lib/application.js:566:10)
at ServerResponse.res.render (/var/www/nodegoogle/node_modules/express/lib/response.js:938:7)
at addData (/var/www/nodegoogle/lib/callbacks/routes.js:63:11)
at /var/www/nodegoogle/lib/callbacks/routes.js:74:13
You're getting this error because the first step of your iteration that reaches addData(req,resp) is sending the response. Remember: the render method ends the response.
If you want to loop through response.messages array and perform an operation for each item before sending the response, you have to make sure to call addDate(req,res) only after the end of your loop.
Take a look at async.each from the async module. An example from the docs:
async.each(openFiles, function( file, callback) {
// Perform operation on file here.
console.log('Processing file ' + file);
if( file.length > 32 ) {
console.log('This file name is too long');
callback('File name too long');
} else {
// Do work to process file here
console.log('File processed');
callback();
}
}, function(err){
// if any of the file processing produced an error, err would equal that error
if( err ) {
// One of the iterations produced an error.
// All processing will now stop.
console.log('A file failed to process');
} else {
console.log('All files have been processed successfully');
}
});
So, in your scenario, you just have to do your stuff on each item (calling the local callback after each one, of course) and call your addData(req,resp) just at the last callback.

Node.JS crashes with "cant set headers after they are sent" error

I have a Node.JS based server. One of the functions of the server is to receive a photo from a mobile client and store it in the mongodb. When there is a bad connection, the file upload process fails, the client receives an error and retries, and everything works as expected, but sometimes, after the upload fails, the server crashes with the "Can't set headers after they are sent" error. Below is the error generated by the server: the user is authenticated and then the upload process fails, PUT /v1/add_photo.json command returns error code 400, but it seems that the remaining functions of the add_photo.json are still called - why? and thus, this error is generated, since at the very end, the send_failure function will set the header again. (When a "successful" upload fail occurs - after add_photo.json returns error code 400, no other functions are called, and everything works as expected)
3/23/2014 16:6:41 0.0.2 handlers/users.js: authenticate_user:
Error: Request aborted
at IncomingMessage <anonymous
(/floomit/node/node/node_modules/express/node_modules/connect/node_modules/multiparty/index.js:93:17)
at IncomingMessage.EventEmitter.emit (events.js:92:17)
at abortIncoming (http.js:1911:11)
at Socket.socket.onend (http.js:2010:7)
at Socket.g (events.js:175:14)
at Socket.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:920:16
at process._tickCallback (node.js:415:13)
PUT /v1/add_photo.json 400 9542ms - 467b
3/23/2014 16:6:41 0.0.2 handlers/streams.js: add_photo:
3/23/2014 16:6:41 0.0.2 data/streams.js: store_file:
3/23/2014 16:6:41 0.0.2 data/streams: store_file: err:
{ [Error: ENOENT, open '/tmp/4684-1425605.jpg'] errno: 34, code: 'ENOENT', path:'/tmp/4684-1425605.jpg' }
3/23/2014 16:6:41 0.0.2 handlers/streams.js: add_photo: err
{ [Error: ENOENT, open '/tmp/4684-1425605.jpg'] errno: 34, code: 'ENOENT', path:'/tmp/4684-1425605.jpg' }
http.js:733
throw new Error('Can\'t render headers after they are sent to the client.'
^
Error: Can't render headers after they are sent to the client.
at ServerResponse.OutgoingMessage._renderHeaders (http.js:733:11)
at ServerResponse.res._renderHeaders
(/floomit/node/node/node_modules/express/node_modules/connect/lib/patch.js:69:27)
at ServerResponse.writeHead (http.js:1150:20)
at ServerResponse.res.writeHead
(/floomit/node/node/node_modules/express/node_modules/connect/lib/patch.js:75:22)
at Object.exports.send_failure (/floomit/node/node/handlers/helpers.js:78:9)
at /floomit/node/node/handlers/streams.js:377:21
at /floomit/node/node/node_modules/async/lib/async.js:428:21
at /floomit/node/node/data/streams.js:495:13
at /floomit/node/node/node_modules/mongodb/lib/mongodb/gridfs/gridstore.js:294:22
at Object.oncomplete (fs.js:107:15)
As can be seen from the log lines above (the lines with date preceding them are printed by my server), the function add_photo is entered, it calls a function store_file, which fails. It seems that the failure happens because the file wasn't successfully uploaded to the server, as indicated by the ENOENT error. Which should just result in an error code being returned to the client and the client retransmitting, but instead the server crashes. This doesn't happen all the time, most of the time the client is returned an error code and retransmits. I event tried modifying the store_file() function to always return the above generated ENOENT error and no crashes occur, the client simply retransmits.
Below is the relevant code from the server:
// stream handler
exports.add_photo = function (req, res) {
var new_photo = new Photo();
async.waterfall([
// store image file
function (cb) {
stream_data.store_file(req.files.file_image.path, cb);
},
function (imageFileId, cb) {
new_photo.imageFileId = imageFileId;
stream_data.add_photo(new_photo, cb);
}
],
function (err, final_photo) {
if (err) {
helpers.floomit_log("handlers/streams.js: add_photo: err");
helpers.floomit_log(err);
helpers.send_failure(res, err);
} else {
helpers.send_success(res, final_photo.response_obj());
}
});
};
// stream_data
exports.store_file = function (file_full_path, callback) {
async.waterfall([
function (cb) {
fileId = db.ObjectID();
fileGS = new db.GridStore(db.db, fileId, "w", {"chunk_size":1024*4});
fileGS.writeFile(file_full_path, cb);
}
],
function (err, results) {
if (err) {
helpers.floomit_log("data/streams: store_file: err:");
helpers.floomit_log(err);
callback(err);
} else {
callback(null, fileId);
}
});
};
// stream_data
exports.add_photo = function (new_photo, callback) {
var query = {};
query["$or"] = [];
async.waterfall([
function (cb) {
new_photo._id = db.ObjectID();
db.photos.insert(new_photo, {w:1}, cb);
},
function (result, cb) {
for (i = 0; i < new_photo.streams.length; i++) {
query["$or"].push({"user":new_photo.streams[i].user,"name":new_photo.streams[i].name});
}
db.streams.update(
query,
{"$inc" : {"photoCount":1}, "$set" : {"timestampU":new_photo.timestamp}},
{w:1, multi:true},
cb);
}
],
function (err) {
if (err) {
helpers.floomit_log("data/streams.js: add_photo: err:");
helpers.floomit_log(err);
callback(err);
} else {
callback(null, new_photo);
}
});
};
// helpers
exports.send_success = function(res, data) {
res.writeHead(200, {"Content-Type": "application/json"});
var output = { error: "none", data: data };
res.end(JSON.stringify(output) + "\n");
};
// helpers
exports.send_failure = function(res, err) {
var code = (err.code) ? err.code : err.name;
res.writeHead(code, { "Content-Type" : "application/json" });
res.end(JSON.stringify({ error: code, message: err.message }) + "\n");
}
The send_failure function does not return proper HTTP error when this occurs, I need to fix this, but that's not where the problem is. (just wanted to point it out so that this issue is not brought up here)
Thank you,
Gary

Categories