Related
I am trying to collect form data with Node.js but I am unable to retrieve it.
I have the following add_device.html page:
<head>
<script>
/* Submit device type form when user clicks on div */
$(function () {
$(".device-form").on("click", function () {
alert(this.device.value);
$(".device-form").submit();
});
});
</script>
</head>
<body>
<div id="devices-available-container">
<% deviceTypes.forEach(function(deviceType){ %>
<div class="device-type-card">
<form class="device-form" method="POST" action="/add_device">
<div class="icon-and-text-container">
<div class="circle"><img class="icon" src="/img/device_types/<%=deviceType.device%>.png"
alt="Lightbulb icon"></div>
<div class="icon-description">
<button name="device" value="<%= deviceType.device %>"
type="text"><%= deviceType.device %></button>
</div>
</div>
</form>
</div>
<% }) %>
</div>
</body>
and in main.js (routes):
app.get("/add_device", function (req, res) {
// query database to get all the books
let sqlquery = "SELECT * FROM available_devices GROUP BY device order by device;";
// execute sql query
db.query(sqlquery, (err, result) => {
if (err) {
console.log(err);
console.log(result);
res.redirect("/");
}
//res.send(result)
console.log(result);
res.render("add_device.html", { deviceTypes: result });
});
});
app.post('/add_device', function (req, res) {
console.log(req.body.device);
});
The rendered HTML is a series of items in a list. When the user clicks on a div containing the item icon and name (yellow box in the image), I want the 'form' to be submitted with the 'device' attribute set to the name of the item clicked.
For example, if the user clicks within the div containing bathtub, the form should be posted and from main.js, req.body.device should equal 'bathtub'.
However, for some reason, the result of console.log(req.body.device); is:
<ref *2> ServerResponse {
_events: [Object: null prototype] { finish: [Function: bound resOnFinish] },
_eventsCount: 1,
_maxListeners: undefined,
outputData: [],
outputSize: 0,
writable: true,
destroyed: false,
_last: false,
chunkedEncoding: false,
shouldKeepAlive: true,
_defaultKeepAlive: true,
useChunkedEncodingByDefault: true,
sendDate: true,
_removedConnection: false,
_removedContLen: false,
_removedTE: false,
_contentLength: null,
_hasBody: true,
_trailer: '',
finished: false,
_headerSent: false,
socket: <ref *1> Socket {
connecting: false,
_hadError: false,
_parent: null,
_host: null,
_readableState: ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: BufferList { head: null, tail: null, length: 0 },
length: 0,
pipes: [],
flowing: true,
ended: false,
endEmitted: false,
reading: true,
sync: false,
needReadable: true,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
errorEmitted: false,
emitClose: false,
autoDestroy: false,
destroyed: false,
errored: null,
closed: false,
closeEmitted: false,
defaultEncoding: 'utf8',
awaitDrainWriters: null,
multiAwaitDrain: false,
readingMore: false,
decoder: null,
encoding: null,
[Symbol(kPaused)]: false
},
_events: [Object: null prototype] {
end: [Array],
timeout: [Function: socketOnTimeout],
data: [Function: bound socketOnData],
error: [Function: socketOnError],
close: [Array],
drain: [Function: bound socketOnDrain],
resume: [Function: onSocketResume],
pause: [Function: onSocketPause]
},
_eventsCount: 8,
_maxListeners: undefined,
_writableState: WritableState {
objectMode: false,
highWaterMark: 16384,
finalCalled: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
destroyed: false,
decodeStrings: false,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: true,
bufferProcessing: false,
onwrite: [Function: bound onwrite],
writecb: null,
writelen: 0,
afterWriteTickInfo: null,
buffered: [],
bufferedIndex: 0,
allBuffers: true,
allNoop: true,
pendingcb: 0,
prefinished: false,
errorEmitted: false,
emitClose: false,
autoDestroy: false,
errored: null,
closed: false,
closeEmitted: false
},
allowHalfOpen: true,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: Server {
maxHeaderSize: undefined,
insecureHTTPParser: undefined,
_events: [Object: null prototype],
_eventsCount: 2,
_maxListeners: undefined,
_connections: 1,
_handle: [TCP],
_usingWorkers: false,
_workers: [],
_unref: false,
allowHalfOpen: true,
pauseOnConnect: false,
httpAllowHalfOpen: false,
timeout: 0,
keepAliveTimeout: 5000,
maxHeadersCount: null,
headersTimeout: 60000,
requestTimeout: 0,
_connectionKey: '6::::8089',
[Symbol(IncomingMessage)]: [Function: IncomingMessage],
[Symbol(ServerResponse)]: [Function: ServerResponse],
[Symbol(kCapture)]: false,
[Symbol(async_id_symbol)]: 12
},
_server: Server {
maxHeaderSize: undefined,
insecureHTTPParser: undefined,
_events: [Object: null prototype],
_eventsCount: 2,
_maxListeners: undefined,
_connections: 1,
_handle: [TCP],
_usingWorkers: false,
_workers: [],
_unref: false,
allowHalfOpen: true,
pauseOnConnect: false,
httpAllowHalfOpen: false,
timeout: 0,
keepAliveTimeout: 5000,
maxHeadersCount: null,
headersTimeout: 60000,
requestTimeout: 0,
_connectionKey: '6::::8089',
[Symbol(IncomingMessage)]: [Function: IncomingMessage],
[Symbol(ServerResponse)]: [Function: ServerResponse],
[Symbol(kCapture)]: false,
[Symbol(async_id_symbol)]: 12
},
parser: HTTPParser {
'0': [Function: bound setRequestTimeout],
'1': [Function: parserOnHeaders],
'2': [Function: parserOnHeadersComplete],
'3': [Function: parserOnBody],
'4': [Function: parserOnMessageComplete],
'5': [Function: bound onParserExecute],
'6': [Function: bound onParserTimeout],
_headers: [],
_url: '',
socket: [Circular *1],
incoming: [IncomingMessage],
outgoing: null,
maxHeaderPairs: 2000,
_consumed: true,
onIncoming: [Function: bound parserOnIncoming],
[Symbol(resource_symbol)]: [HTTPServerAsyncResource]
},
on: [Function: socketListenerWrap],
addListener: [Function: socketListenerWrap],
prependListener: [Function: socketListenerWrap],
_paused: false,
_httpMessage: [Circular *2],
[Symbol(async_id_symbol)]: 15351,
[Symbol(kHandle)]: TCP {
reading: true,
onconnection: null,
_consumed: true,
[Symbol(owner_symbol)]: [Circular *1]
},
[Symbol(kSetNoDelay)]: false,
[Symbol(lastWriteQueueSize)]: 0,
[Symbol(timeout)]: null,
[Symbol(kBuffer)]: null,
[Symbol(kBufferCb)]: null,
[Symbol(kBufferGen)]: null,
[Symbol(kCapture)]: false,
[Symbol(kBytesRead)]: 0,
[Symbol(kBytesWritten)]: 0,
[Symbol(RequestTimeout)]: undefined
},
_header: null,
_keepAliveTimeout: 5000,
_onPendingData: [Function: bound updateOutgoingData],
_sent100: false,
_expect_continue: false,
req: IncomingMessage {
_readableState: ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: BufferList { head: null, tail: null, length: 0 },
length: 0,
pipes: [],
flowing: true,
ended: true,
endEmitted: true,
reading: false,
sync: true,
needReadable: false,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
errorEmitted: false,
emitClose: true,
autoDestroy: false,
destroyed: false,
errored: null,
closed: false,
closeEmitted: false,
defaultEncoding: 'utf8',
awaitDrainWriters: null,
multiAwaitDrain: false,
readingMore: true,
decoder: null,
encoding: null,
[Symbol(kPaused)]: false
},
_events: [Object: null prototype] { end: [Function: clearRequestTimeout] },
_eventsCount: 1,
_maxListeners: undefined,
socket: <ref *1> Socket {
connecting: false,
_hadError: false,
_parent: null,
_host: null,
_readableState: [ReadableState],
_events: [Object: null prototype],
_eventsCount: 8,
_maxListeners: undefined,
_writableState: [WritableState],
allowHalfOpen: true,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: [Server],
_server: [Server],
parser: [HTTPParser],
on: [Function: socketListenerWrap],
addListener: [Function: socketListenerWrap],
prependListener: [Function: socketListenerWrap],
_paused: false,
_httpMessage: [Circular *2],
[Symbol(async_id_symbol)]: 15351,
[Symbol(kHandle)]: [TCP],
[Symbol(kSetNoDelay)]: false,
[Symbol(lastWriteQueueSize)]: 0,
[Symbol(timeout)]: null,
[Symbol(kBuffer)]: null,
[Symbol(kBufferCb)]: null,
[Symbol(kBufferGen)]: null,
[Symbol(kCapture)]: false,
[Symbol(kBytesRead)]: 0,
[Symbol(kBytesWritten)]: 0,
[Symbol(RequestTimeout)]: undefined
},
httpVersionMajor: 1,
httpVersionMinor: 1,
httpVersion: '1.1',
complete: true,
headers: {
host: 'localhost:8089',
connection: 'keep-alive',
'content-length': '0',
'cache-control': 'max-age=0',
'upgrade-insecure-requests': '1',
origin: 'http://localhost:8089',
'content-type': 'application/x-www-form-urlencoded',
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/88.0.4324.150 Safari/537.36',
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'sec-fetch-site': 'same-origin',
'sec-fetch-mode': 'navigate',
'sec-fetch-user': '?1',
'sec-fetch-dest': 'document',
referer: 'http://localhost:8089/add_device',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US'
},
rawHeaders: [
'Host',
'localhost:8089',
'Connection',
'keep-alive',
'Content-Length',
'0',
'Cache-Control',
'max-age=0',
'Upgrade-Insecure-Requests',
'1',
'Origin',
'http://localhost:8089',
'Content-Type',
'application/x-www-form-urlencoded',
'User-Agent',
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/88.0.4324.150 Safari/537.36',
'Accept',
'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'Sec-Fetch-Site',
'same-origin',
'Sec-Fetch-Mode',
'navigate',
'Sec-Fetch-User',
'?1',
'Sec-Fetch-Dest',
'document',
'Referer',
'http://localhost:8089/add_device',
'Accept-Encoding',
'gzip, deflate, br',
'Accept-Language',
'en-US'
],
trailers: {},
rawTrailers: [],
aborted: false,
upgrade: false,
url: '/add_device',
method: 'POST',
statusCode: null,
statusMessage: null,
client: <ref *1> Socket {
connecting: false,
_hadError: false,
_parent: null,
_host: null,
_readableState: [ReadableState],
_events: [Object: null prototype],
_eventsCount: 8,
_maxListeners: undefined,
_writableState: [WritableState],
allowHalfOpen: true,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: [Server],
_server: [Server],
parser: [HTTPParser],
on: [Function: socketListenerWrap],
addListener: [Function: socketListenerWrap],
prependListener: [Function: socketListenerWrap],
_paused: false,
_httpMessage: [Circular *2],
[Symbol(async_id_symbol)]: 15351,
[Symbol(kHandle)]: [TCP],
[Symbol(kSetNoDelay)]: false,
[Symbol(lastWriteQueueSize)]: 0,
[Symbol(timeout)]: null,
[Symbol(kBuffer)]: null,
[Symbol(kBufferCb)]: null,
[Symbol(kBufferGen)]: null,
[Symbol(kCapture)]: false,
[Symbol(kBytesRead)]: 0,
[Symbol(kBytesWritten)]: 0,
[Symbol(RequestTimeout)]: undefined
},
_consuming: false,
_dumped: false,
next: [Function: next],
baseUrl: '',
originalUrl: '/add_device',
_parsedUrl: Url {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: null,
query: null,
pathname: '/add_device',
path: '/add_device',
href: '/add_device',
_raw: '/add_device'
},
params: {},
query: {},
res: [Circular *2],
body: {},
_body: true,
length: undefined,
route: Route { path: '/add_device', stack: [Array], methods: [Object] },
[Symbol(kCapture)]: false,
[Symbol(RequestTimeout)]: undefined
},
locals: [Object: null prototype] {},
[Symbol(kCapture)]: false,
[Symbol(kNeedDrain)]: false,
[Symbol(corked)]: 0,
[Symbol(kOutHeaders)]: [Object: null prototype] {
'x-powered-by': [ 'X-Powered-By', 'Express' ]
}
}
What I would like to have when printing console.log(req.body.device); is something like { device: dviceNameClickedByTheUser } Any help would be much appreciated, thanks!
Make sure you are using express.urlencoded({ extended: true }) middleware,
as shown in the documentation otherwise req.body will be undefined.
The output of console.log(req.body.device) you posted is probably the result of outdated code running, because that is what you would normally get from running console.log(req).
I've tried running your code and the console output works correctly
https://www.anyfiddle.com/p/robalb/sih511dv
Also this is not relevant to the question, but you can get rid of the javascript code in your template by changing the type of the form button from type="text" to type="submit" like this
<button name="device" value="<%= deviceType.device %>"
type="submit"><%= deviceType.device %></button>
Resolved by using hidden input tag:
<script>
/* Submit device type form when user clicks on div */
$(function () {
$(".device-form").on("click", function () {
$(this).submit();
});
});
</script>
<body>
<div id="devices-available-container">
<% deviceTypes.forEach(function(deviceType){ %>
<div class="device-type-card">
<form class="device-form" method="POST" action="/add_device">
<input input type="hidden" id="first" type="text" name="device" value="<%= deviceType.device %>" />
<div class="icon-and-text-container">
<div class="circle"><img class="icon" src="/img/device_types/<%=deviceType.device%>.png"
alt="Lightbulb icon"></div>
<div class="icon-description">
<p><%= deviceType.device %></p>
</div>
</div>
</form>
</div>
<% }) %>
</div>
</body>
I'm new in node.js. I using AWS with node.js and MySQL to create a lambda function, but I can't catch the results in my handler, my database instance it's in RDS service.
When I launch my callback in the console it shows me a giant and unreadable json with information from the database but not from the records that I am consulting, why is this? because it happens, am I doing something wrong?
This is my code:
'use strict';
const AWS = require("aws-sdk");
AWS.config.update( { region: "us-west-2" } );
var mysql = require('mysql');
var querystring = require('querystring');
const con = mysql.createConnection({
host : process.env.RDS_HOSTNAME,
user : process.env.RDS_USERNAME,
password : process.env.RDS_PASSWORD,
database : process.env.RDS_DB_NAME
});
exports.handler = async (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;
const results = await con.query('SELECT * FROM `documentoApp_rptenviadoclon`;');
await con.end();
if (results) {
callback(null, {
statusCode: 200,
headers: {
'Access-Control-Allow-Headers': 'Content-Type,X-Amz-Date,Authorization,X-Requested-With',
'Access-Control-Allow-Methods': 'GET,POST,OPTIONS',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify(results)
});
} else {
callback('error', {
statusCode: 400,
headers: {
'Access-Control-Allow-Headers': 'Content-Type,X-Amz-Date,Authorization,X-Requested-With',
'Access-Control-Allow-Methods': 'GET,POST,OPTIONS',
'Access-Control-Allow-Origin': '*'
},
body: {
message: 'No results found.'
},
});
}
};
I tried to put a console.log in response body and this is what it shows, the giant and illegible json of which I speak:
START RequestId: 80dfacdb-034b-41bc-8c53-e5eb3347d035 Version: $LATEST
2020-03-20T21:57:15.134Z 80dfacdb-034b-41bc-8c53-e5eb3347d035 INFO Query {
_events: [Object: null prototype] {
error: [Function],
packet: [Function],
timeout: [Function],
end: [Function]
},
_eventsCount: 4,
_maxListeners: undefined,
_callback: undefined,
_callSite: Error
at Protocol._enqueue (/var/task/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Connection.query (/var/task/node_modules/mysql/lib/Connection.js:198:25)
at Runtime.exports.handler (/var/task/index.js:19:31)
at Runtime.handleOnce (/var/runtime/Runtime.js:66:25),
_ended: false,
_timeout: undefined,
_timer: Timer { _object: [Circular], _timeout: null },
sql: 'SELECT * FROM `documentoApp_rptenviadoclon`;',
values: undefined,
typeCast: true,
nestTables: false,
_resultSet: null,
_results: [],
_fields: [],
_index: 0,
_loadError: null,
_connection: Connection {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
config: ConnectionConfig {
host: '--hidden--',
port: 3306,
localAddress: undefined,
socketPath: undefined,
user: '--hidden--',
password: '--hidden--',
database: '--hidden--',
connectTimeout: 10000,
insecureAuth: false,
supportBigNumbers: false,
bigNumberStrings: false,
dateStrings: false,
debug: undefined,
trace: true,
stringifyObjects: false,
timezone: 'local',
flags: '',
queryFormat: undefined,
pool: undefined,
ssl: false,
localInfile: true,
multipleStatements: false,
typeCast: true,
maxPacketSize: 0,
charsetNumber: 33,
clientFlags: 455631
},
_socket: Socket {
connecting: true,
_hadError: false,
_parent: null,
_host: 'dbportalreportes-dev.cjawt1xkypqu.us-west-2.rds.amazonaws.com',
_readableState: [ReadableState],
readable: false,
_events: [Object: null prototype],
_eventsCount: 5,
_maxListeners: undefined,
_writableState: [WritableState],
writable: true,
allowHalfOpen: false,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: null,
_server: null,
timeout: 10000,
[Symbol(asyncId)]: 3,
[Symbol(kHandle)]: [TCP],
[Symbol(lastWriteQueueSize)]: 0,
[Symbol(timeout)]: Timeout {
_idleTimeout: 10000,
_idlePrev: [TimersList],
_idleNext: [TimersList],
_idleStart: 469,
_onTimeout: [Function: bound ],
_timerArgs: undefined,
_repeat: null,
_destroyed: false,
[Symbol(refed)]: false,
[Symbol(asyncId)]: 7,
[Symbol(triggerId)]: 0
},
[Symbol(kBuffer)]: null,
[Symbol(kBufferCb)]: null,
[Symbol(kBufferGen)]: null,
[Symbol(kBytesRead)]: 0,
[Symbol(kBytesWritten)]: 0
},
_protocol: Protocol {
_events: [Object: null prototype],
_eventsCount: 7,
_maxListeners: undefined,
readable: true,
writable: true,
_config: [ConnectionConfig],
_connection: [Circular],
_callback: null,
_fatalError: null,
_quitSequence: [Quit],
_handshake: true,
_handshaked: false,
_ended: false,
_destroyed: false,
_queue: [Array],
_handshakeInitializationPacket: null,
_parser: [Parser]
},
_connectCalled: true,
state: 'disconnected',
threadId: null
}
}
END RequestId: 80dfacdb-034b-41bc-8c53-e5eb3347d035
REPORT RequestId: 80dfacdb-034b-41bc-8c53-e5eb3347d035 Duration: 208.98 ms Billed Duration: 300 ms Memory Size: 128 MB Max Memory Used: 92 MB Init Duration: 415.02 ms
I hope you can help me, thank you very much!
Here is my JSON response. I want to parse the below JSON and get Site_id from header.location. I am able to get Status, Message values but not location value.
It is actually response header, I am trying to parse.
Please, anyone can help here?
var apigClientFactory = require('aws-api-gateway-client').default;
let awsbody = JSON.parse(process.argv[2].split('\\').join('') || '{}');
var apigClient = apigClientFactory.newClient({
invokeUrl: awsbody.endPoint, // REQUIRED
accessKey: awsbody.awsAccessKey, // REQUIRED
secretKey: awsbody.awsSecreteKey, // REQUIRED
region: awsbody.awsRegion, // REQUIRED: The region where the AapiKeyloyed.
retryCondition: (err) => { // OPTIONAL: Callback to further control if request should be retried. Uses axon-
retry plugin.
return err.response && err.response.status === 500;
}
});
var pathParams = awsbody.awsPathParams;
// Template syntax follows url-template https://www.npmjs.com/package/url-template
var pathTemplate = awsbody.awsPathTemplate; // '/api/v1/sites'
var method = awsbody.method; // 'POST';
var additionalParams = awsbody.awsAdditionalParams; //queryParams & Headers if any
//var additionalParams = {headers: { "x-apigw-api-id": 'vs2i50xvo4'}};
var body = awsbody.requestBody;
var output = {};
apigClient.invokeApi(pathParams, pathTemplate, method, additionalParams, body)
.then(function(result) {
console.log(result)
}).catch(function(result) {
console.log(result)
});
Here is output: But I just need Status, StatusText, location under headers and data (# bottom) as output. I am tried to parse the output in above code no luck.
Can anyone help to parse response.
{ status: 202,
statusText: 'Accepted',
headers:
{ server: 'Server',
date: 'Tue, 12 Mar 2019 20:34:36 GMT',
'content-type': 'application/json',
'content-length': '61',
'x-amzn-requestid': '400377cd-4506-11e9-b941-3984677a719c',
'x-amz-apigw-id': 'WciqVH8pPHcFgWg=',
location:
'/transit-connectivity/api/v1/jobs/8125d985-3d90-473d-b799-c9974
'x-amzn-trace-id': 'Root=1-5c8817db-5522b10bf2068ac2509ae35b;Samp
connection: 'close' },
config:
{ adapter: [Function: httpAdapter],
transformRequest: { '0': [Function: transformRequest] },
transformResponse: { '0': [Function: transformResponse] },
timeout: 0,
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
validateStatus: [Function: validateStatus],
headers:
{ Accept: 'application/json',
'Content-Type': 'application/json',
'x-apigw-api-id': 'vs2i50xvo4',
'x-amz-date': '20190312T203427Z',
Authorization:
'AWS4-HMAC-SHA256 Credential=AKIAJ4Y5DGRDNBQ2HBYA/20190312/us
cept;content-type;host;x-amz-date;x-apigw-api-id, Signature=801d0887fd
a249',
'User-Agent': 'axios/0.18.0',
'Content-Length': 245 },
method: 'post',
data:
'{"site_id":"TDCLOUDTSTTD03","account_id":"109443356002","change
ider":"aws","region":"us-west-2","network_id":"vpc-0ab26280d5060105c",
01.128/25"]}',
url:
'https://vpce-09d3e2325f36b5527-8xogk1r2.execute-api.us-west-2.v
ites' },
request:
ClientRequest {
_events:
[Object: null prototype] {
socket: [Function],
abort: [Function],
aborted: [Function],
error: [Function],
timeout: [Function],
prefinish: [Function: requestOnPrefinish] },
_eventsCount: 6,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 0,
writable: true,
_last: true,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: true,
sendDate: false,
_removedConnection: false,
_removedContLen: false,
_removedTE: false,
_contentLength: null,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket:
TLSSocket {
_tlsOptions: [Object],
_secureEstablished: true,
_securePending: false,
_newSessionPending: false,
_controlReleased: true,
_SNICallback: null,
servername:
'vpce-09d3e2325f36b5527-8xogk1r2.execute-api.us-west-2.vpce.a
alpnProtocol: false,
authorized: true,
authorizationError: null,
encrypted: true,
_events: [Object],
_eventsCount: 8,
connecting: false,
_hadError: false,
_handle: [TLSWrap],
_parent: null,
_host:
'vpce-09d3e2325f36b5527-8xogk1r2.execute-api.us-west-2.vpce.a
_readableState: [ReadableState],
readable: true,
_maxListeners: undefined,
_writableState: [WritableState],
writable: false,
allowHalfOpen: false,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: undefined,
_server: null,
ssl: [TLSWrap],
_requestCert: true,
_rejectUnauthorized: true,
parser: null,
_httpMessage: [Circular],
[Symbol(res)]: [TLSWrap],
[Symbol(asyncId)]: 6,
[Symbol(lastWriteQueueSize)]: 0,
[Symbol(timeout)]: null,
[Symbol(kBytesRead)]: 0,
[Symbol(kBytesWritten)]: 0,
[Symbol(connect-options)]: [Object] },
connection:
TLSSocket {
_tlsOptions: [Object],
_secureEstablished: true,
_securePending: false,
_newSessionPending: false,
_controlReleased: true,
_SNICallback: null,
servername:
'vpce-09d3e2325f36b5527-8xogk1r2.execute-api.us-west-2.vpce.a
alpnProtocol: false,
authorized: true,
authorizationError: null,
encrypted: true,
_events: [Object],
_eventsCount: 8,
connecting: false,
_hadError: false,
_handle: [TLSWrap],
_parent: null,
_host:
'vpce-09d3e2325f36b5527-8xogk1r2.execute-api.us-west-2.vpce.a
_readableState: [ReadableState],
readable: true,
_maxListeners: undefined,
_writableState: [WritableState],
writable: false,
allowHalfOpen: false,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: undefined,
_server: null,
ssl: [TLSWrap],
_requestCert: true,
_rejectUnauthorized: true,
parser: null,
_httpMessage: [Circular],
[Symbol(res)]: [TLSWrap],
[Symbol(asyncId)]: 6,
[Symbol(lastWriteQueueSize)]: 0,
[Symbol(timeout)]: null,
[Symbol(kBytesRead)]: 0,
[Symbol(kBytesWritten)]: 0,
[Symbol(connect-options)]: [Object] },
_header:
'POST /qa/transit-connectivity/api/v1/sites HTTP/1.1\r\nAccept:
\r\nx-apigw-api-id: vs2i50xvo4\r\nx-amz-date: 20190312T203427Z\r\nAuth
DNBQ2HBYA/20190312/us-west-2/execute-api/aws4_request, SignedHeaders=a
Signature=801d0887fd4a8b86a1af51a02cede529962fc5cae84f4a10f23983100e0
h: 245\r\nHost: vpce-09d3e2325f36b5527-8xogk1r2.execute-api.us-west-2.
_onPendingData: [Function: noopPendingOutput],
agent:
Agent {
_events: [Object],
_eventsCount: 1,
_maxListeners: undefined,
defaultPort: 443,
protocol: 'https:',
options: [Object],
requests: {},
sockets: [Object],
freeSockets: {},
keepAliveMsecs: 1000,
keepAlive: false,
maxSockets: Infinity,
maxFreeSockets: 256,
maxCachedSessions: 100,
_sessionCache: [Object] },
socketPath: undefined,
timeout: undefined,
method: 'POST',
path: '/qa/transit-connectivity/api/v1/sites',
_ended: true,
res:
IncomingMessage {
_readableState: [ReadableState],
readable: false,
_events: [Object],
_eventsCount: 3,
_maxListeners: undefined,
socket: [TLSSocket],
connection: [TLSSocket],
httpVersionMajor: 1,
httpVersionMinor: 1,
httpVersion: '1.1',
complete: true,
headers: [Object],
rawHeaders: [Array],
trailers: {},
rawTrailers: [],
aborted: false,
upgrade: false,
url: '',
method: null,
statusCode: 202,
statusMessage: 'Accepted',
client: [TLSSocket],
_consuming: true,
_dumped: false,
req: [Circular],
responseUrl:
'https://vpce-09d3e2325f36b5527-8xogk1r2.execute-api.us-west-
1/sites',
redirects: [] },
aborted: undefined,
timeoutCb: null,
upgradeOrConnect: false,
parser: null,
maxHeadersCount: null,
_redirectable:
Writable {
_writableState: [WritableState],
writable: true,
_events: [Object],
_eventsCount: 2,
_maxListeners: undefined,
_options: [Object],
_redirectCount: 0,
_redirects: [],
_requestBodyLength: 245,
_requestBodyBuffers: [],
_onNativeResponse: [Function],
_currentRequest: [Circular],
_currentUrl:
'https://vpce-09d3e2325f36b5527-8xogk1r2.execute-api.us-west-
1/sites' },
[Symbol(isCorked)]: false,
[Symbol(outHeadersKey)]:
[Object: null prototype] {
accept: [Array],
'content-type': [Array],
'x-apigw-api-id': [Array],
'x-amz-date': [Array],
authorization: [Array],
'user-agent': [Array],
'content-length': [Array],
host: [Array] } },
data: { status: 'success', message: 'request has been accepted' } }
varible result is not in json format actually, take a look at my code which is in valid Json format.
You can get location as you need now.
let v = '{"data": {"status": "sucess","message": "request has been accepted"},"responsecode": 202,"responsetext": "Accepted","headers": {"server": "Server","date": "Tue, 12 Mar 2019 20:34:36 GMT","content-type": "application/json","content-length": "61","x-amzn-requestid": "400377cd-4506-11e9-b941-3984677a719c","x-amz-apigw-id": "WciqVH8pPHcFgWg=","location": "/transit-connectivity/api/v1/jobs/8125d985-3d90-473d-b799-c9974","x-amzn-trace-id": "Root=1-5c8817db-5522b10bf2068ac2509ae35b;Sampled=0","connection": "close"}}';
let value = JSON.parse(v);
console.log("location :"value.headers.location);
Console:
location : /transit-connectivity/api/v1/jobs/8125d985-3d90-473d-b799-c9974
The url https://mobileapp.mydomain.com/api/uuids?filter[where][uuid]=02644da038b37d7ba70b7ee1a92ba1d9output is
[{"uuid":"02644da038b37d7ba70b7ee1a92ba1d9","date":"2017/10/05","time":"8:31:41","verified":false,"token":"null","id":"59d5bcb52f416812b0fc817a"}]
In following code https.get's result is not json like and process.stdout.write(d)?
How can I get output in json format with less process.
const https = require('https');
var URL = 'https://mobileapp.mydomain.com/api/uuids?filter[where][uuid]=02644da038b37d7ba70b7ee1a92ba1d9';
https.get(URL, (res) => {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
res.on('data', (d) => {
process.stdout.write(d);
});
}).on('error', (e) => {
console.error('ERROR:',e);
});
update output
output of process.stdout.write(d); :
ClientRequest {
domain:
Domain {
domain: null,
_events: { error: [Function: debugDomainError] },
_eventsCount: 1,
_maxListeners: undefined,
members: [] },
_events:
{ response: { [Function: bound onceWrapper] listener: [Function] },
socket: { [Function: bound onceWrapper] listener: [Function: onSocket] },
error: [Function] },
_eventsCount: 3,
_maxListeners: undefined,
output: [ 'GET /api/uuids?filter[where][uuid]=02644da038b37d7ba70b7ee1a92ba1d9 HTTP/1.1\r\nHost: mobileapp.mydomain.com\r\nConnection: close\r\n\r\n' ],
outputEncodings: [ 'latin1' ],
outputCallbacks: [ [Function: bound onFinish] ],
outputSize: 128,
writable: false,
_last: true,
upgrading: false,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: false,
sendDate: false,
_removedConnection: false,
_removedContLen: false,
_removedTE: false,
_contentLength: 0,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket: null,
connection: null,
_header: 'GET /api/uuids?filter[where][uuid]=02644da038b37d7ba70b7ee1a92ba1d9 HTTP/1.1\r\nHost: mobileapp.mydomain.com\r\nConnection: close\r\n\r\n',
_onPendingData: [Function: noopPendingOutput],
agent:
Agent {
domain:
Domain {
domain: null,
_events: [Object],
_eventsCount: 1,
_maxListeners: undefined,
members: [] },
_events: { free: [Function] },
_eventsCount: 1,
_maxListeners: undefined,
defaultPort: 443,
protocol: 'https:',
options: { path: null },
requests: {},
sockets: { 'mobileapp.mydomain.com:443:::::::::': [Array] },
freeSockets: {},
keepAliveMsecs: 1000,
keepAlive: false,
maxSockets: Infinity,
maxFreeSockets: 256,
maxCachedSessions: 100,
_sessionCache: { map: {}, list: [] } },
socketPath: undefined,
timeout: undefined,
method: 'GET',
path: '/api/uuids?filter[where][uuid]=02644da038b37d7ba70b7ee1a92ba1d9',
_ended: false,
res: null,
aborted: undefined,
timeoutCb: null,
upgradeOrConnect: false,
parser: null,
maxHeadersCount: null,
[Symbol(outHeadersKey)]: { host: [ 'Host', 'mobileapp.mydomain.com' ] } }
> statusCode: 200
headers: { server: 'nginx/1.13.5',
date: 'Thu, 05 Oct 2017 12:43:58 GMT',
'content-type': 'application/json; charset=utf-8',
'content-length': '146',
connection: 'close',
vary: 'Accept-Encoding, Origin, Accept-Encoding',
'access-control-allow-credentials': 'true',
'x-xss-protection': '1; mode=block',
'x-frame-options': 'DENY',
'strict-transport-security': 'max-age=0; includeSubDomains, max-age=31536000',
'x-download-options': 'noopen',
'x-content-type-options': 'nosniff',
etag: 'W/"92-jcLXCzK/YpUyGdSTWS1c4YsxlvU"' }
[{"uuid":"02644da038b37d7ba70b7ee1a92ba1d9","date":"2017/10/05","time":"8:31:41","verified":false,"token":"null","id":"59d5bcb52f416812b0fc817a"}]>
I am querying something that I have done multiple times. Take a look, I can't seem to understand.
/* GET all things */
app.get('/things', function(req, res) {
var search = req.query.search;
console.log(search); //works properly and prints strings
Thing.find({ name: {$regex : search}}, function(err, val) {
if(err) throw error;
console.log(val); // returns some horrible JSON
res.send(JSON.stringify(val)); // Throws TypeError
});
})
I thought maybe my query was wrong and that perhaps maybe it's the Mongo shell throwing issues, but when I went into the Mongo shell.
use dbname
> db.booking.find({name: {$regex: "G"}})
>{ "_id" : ObjectId("58238283565e2c1940b16d48"), "name" : "Go to Lesters"}
This is what happens when I print val.
Readable {
pool: null,
server: null,
disconnectHandler:
{ s: { storedOps: [], storeOptions: [Object], topology: [Object] },
length: [Getter] },
bson: {},
ns: 'vectio.booking',
cmd:
{ find: 'vectio.booking',
limit: 0,
skip: 0,
query: { name: [Object] },
slaveOk: true,
readPreference: { preference: 'primary', tags: undefined, options: [Object] } },
options:
{ skip: 0,
limit: 0,
raw: undefined,
hint: null,
timeout: undefined,
slaveOk: true,
readPreference: { preference: 'primary', tags: undefined, options: [Object] },
db:
EventEmitter {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
s: [Object],
serverConfig: [Getter],
bufferMaxEntries: [Getter],
databaseName: [Getter] },
promiseLibrary: [Function: Promise],
disconnectHandler: { s: [Object], length: [Getter] } },
topology:
EventEmitter {
domain: null,
_events:
{ reconnect: [Function],
reconnectFailed: [Function],
timeout: [Object],
error: [Object],
close: [Function],
destroy: [Object],
serverDescriptionChanged: [Function],
serverHeartbeatStarted: [Function],
serverHeartbeatSucceeded: [Function],
serverHeartbeatFailed: [Function],
serverOpening: [Function],
serverClosed: [Function],
topologyOpening: [Function],
topologyClosed: [Function],
topologyDescriptionChanged: [Function],
attemptReconnect: [Function],
monitoring: [Function] },
_eventsCount: 17,
_maxListeners: undefined,
id: 0,
s:
{ options: [Object],
logger: [Object],
Cursor: [Object],
bson: {},
pool: [Object],
disconnectHandler: [Object],
monitoring: true,
inTopology: false,
monitoringInterval: 5000,
topologyId: -1 },
ismaster:
{ ismaster: true,
maxBsonObjectSize: 16777216,
maxMessageSizeBytes: 48000000,
maxWriteBatchSize: 1000,
localTime: 2016-11-09T20:41:56.152Z,
maxWireVersion: 2,
minWireVersion: 0,
ok: 1 },
lastIsMasterMS: 6,
monitoringProcessId:
Timeout {
_called: false,
_idleTimeout: 5000,
_idlePrev: [Object],
_idleNext: [Object],
_idleStart: 255,
_onTimeout: [Function],
_repeat: null },
initalConnect: false,
wireProtocolHandler: {},
_type: 'server',
clientInfo:
{ driver: [Object],
os: [Object],
platform: 'Node.js v6.2.1, LE, mongodb-core: 2.0.13' },
lastUpdateTime: 0,
lastWriteDate: 0,
staleness: 0 },
cursorState:
{ cursorId: null,
cmd:
{ find: 'vectio.booking',
limit: 0,
skip: 0,
query: [Object],
slaveOk: true,
readPreference: [Object] },
documents: [],
cursorIndex: 0,
dead: false,
killed: false,
init: false,
notified: false,
limit: 0,
skip: 0,
batchSize: 1000,
currentLimit: 0,
transforms: undefined },
logger: { className: 'Cursor' },
_readableState:
ReadableState {
objectMode: true,
highWaterMark: 16,
buffer: [],
length: 0,
pipes: null,
pipesCount: 0,
flowing: null,
ended: false,
endEmitted: false,
reading: false,
sync: true,
needReadable: false,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: true,
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
s:
{ numberOfRetries: 5,
tailableRetryInterval: 500,
currentNumberOfRetries: 5,
state: 0,
streamOptions: {},
bson: {},
ns: 'vectio.booking',
cmd:
{ find: 'vectio.booking',
limit: 0,
skip: 0,
query: [Object],
slaveOk: true,
readPreference: [Object] },
options:
{ skip: 0,
limit: 0,
raw: undefined,
hint: null,
timeout: undefined,
slaveOk: true,
readPreference: [Object],
db: [Object],
promiseLibrary: [Function: Promise],
disconnectHandler: [Object] },
topology:
EventEmitter {
domain: null,
_events: [Object],
_eventsCount: 17,
_maxListeners: undefined,
id: 0,
s: [Object],
ismaster: [Object],
lastIsMasterMS: 6,
monitoringProcessId: [Object],
initalConnect: false,
wireProtocolHandler: {},
_type: 'server',
clientInfo: [Object],
lastUpdateTime: 0,
lastWriteDate: 0,
staleness: 0 },
topologyOptions:
{ host: 'localhost',
port: 27017,
disconnectHandler: [Object],
cursorFactory: [Object],
reconnect: true,
emitError: true,
size: 5,
socketOptions: {},
clientInfo: [Object],
readPreference: [Object],
promiseLibrary: [Function: Promise],
bson: {} },
promiseLibrary: [Function: Promise],
currentDoc: null },
sortValue: undefined }
/home/dilraj/Documents/Vectio/scenario-dilraj/node_modules/mongodb/lib/utils.js:99
process.nextTick(function() { throw err; });
^
TypeError: Converting circular structure to JSON
I am trying to get something like
{
_id: ObjectId("58238283565e2c1940b16d48"),
name: "Go to Lesters"
}
or something similar, as long as it makes sense! I just haven't seen anything like this.
I am using Node's Express JS framework, and the only modules I have are Nodemon, Express, bodyParser and Mongo. Nothing else! Thank you, appreciate the love!
The object you are logging is the Mongo cursor. This can be useful at times but to just get the document that is not what you need.
Thing.find(query).toArray(function(err, result){ ... })
Should just return the documents that match your query.
If you request returns a single object, you can also use
Thing.findOne({ name: {$regex : search}}, function(err, val) {
You can use the same syntax but you need to handle the promise using .then
or use async await