inserts not working mongodb - node.js

I'm having a problem while using a webtask as a backend web service to a static website. I'm using the web service to insert values to a mongodb database. The inserts are not returning any errors, but there is nothing inserting into the database. It just says 'worked' on the response. Here's the relevant code:
var MongoClient = require('mongodb').MongoClient;
function savePage (page, db, cb){
var doc = {
pageSec: page
};
db.collection("page").insertOne(doc, function (err) {
if(err) {return cb(err);}
cb('worked');
});
}
module.exports = function(context, cb){
var section = context.query.section;
if(section){
MongoClient.connect(context.data.mongoUri, function(err, db) {
if(err) {return cb(err);}
savePage(section, db, function (err) {
if(err){return cb(err);}
cb(null);
});
});
}
};
additionally, this is what mongodb returns in response to the insert
"details": {
"ok": 1,
"n": 1,
"lastOp": "6307485423539060737",
"electionId": "576dec4e2c52240b7a5bca5e"
},
"message": {
"parsed": true,
"index": 96,
"raw": {
"type": "Buffer",
"data": [
96,
0,
0,
0,
163,
69,
36,
3,
5,
0,
0,
0,
1,
0,
0,
0,
8,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1,
0,
0,
0,
60,
0,
0,
0,
16,
111,
107,
0,
1,
0,
0,
0,
16,
110,
0,
1,
0,
0,
0,
17,
108,
97,
115,
116,
79,
112,
0,
1,
0,
0,
0,
147,
176,
136,
87,
7,
101,
108,
101,
99,
116,
105,
111,
110,
73,
100,
0,
87,
109,
236,
78,
44,
82,
36,
11,
122,
91,
202,
94,
0
]
},
"data": {
"type": "Buffer",
"data": [
96,
0,
0,
0,
163,
69,
36,
3,
5,
0,
0,
0,
1,
0,
0,
0,
8,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1,
0,
0,
0,
60,
0,
0,
0,
16,
111,
107,
0,
1,
0,
0,
0,
16,
110,
0,
1,
0,
0,
0,
17,
108,
97,
115,
116,
79,
112,
0,
1,
0,
0,
0,
147,
176,
136,
87,
7,
101,
108,
101,
99,
116,
105,
111,
110,
73,
100,
0,
87,
109,
236,
78,
44,
82,
36,
11,
122,
91,
202,
94,
0
]
},
"bson": {},
"opts": {
"promoteLongs": true
},
"length": 96,
"requestId": 52708771,
"responseTo": 5,
"responseFlags": 8,
"cursorId": "0",
"startingFrom": 0,
"numberReturned": 1,
"documents": [
{
"ok": 1,
"n": 1,
"lastOp": "6307485423539060737",
"electionId": "576dec4e2c52240b7a5bca5e"
}
],
"cursorNotFound": false,
"queryFailure": false,
"shardConfigStale": false,
"awaitCapable": true,
"promoteLongs": true
}

When I build my JSON object to sent to MongoDB, I always set the property name as string and it works.
var doc = {
"pageSec": page
};
I don't see anything wrong in your code, so you can try this.

Related

Process stdout capture for Autoruns

Hey guys I am working on a project currently where I am trying to run Autorunsc64.exe from the sysinternals suite and, unlike other executables I have tried, I can't get it to give me the output in a form I want. Here is the current iteration of my code:
use std::process::{Command as process_command, Stdio};
fn autorun_programs() -> String {
// Check where sysinternals is developer vs release
let full_exe_path = current_exe().unwrap();
let mut split_exe_path: Vec<&str> = Vec::new();
if full_exe_path.to_str().unwrap().contains("target") {
let temp: Vec<&str> = full_exe_path.to_str().unwrap().split("system_recon\\target\\debug\\system_recon.exe").collect();
split_exe_path.push(temp[0]);
} else {
let temp: Vec<&str> = full_exe_path.to_str().unwrap().split("system_recon.exe").collect();
split_exe_path.push(temp[0]);
};
let partial_exe_path = split_exe_path[0].to_string();
let sysinternals_exe_string = partial_exe_path + &"SysinternalsSuite\\Autorunsc64.exe".to_string();
//my_command.args(["-nobanner", "/accepteula", "-a *", "-c", "-h", "-s", "-v", "-vt", "*"]);
let mut command = process_command::new(sysinternals_exe_string);
command.arg("-nobanner");
command.arg("-accepteula");
//command.arg("-x");
command.arg("-t");
//command.arg("-a");
//command.arg("*");
//command.arg("-x");
//command.arg("-h");
//command.arg("-s");
//command.arg("-v");
//command.arg("-vt");
//command.arg("*");
command.stdout(Stdio::piped());
command.stderr(Stdio::piped());
let output = command.execute_output().unwrap();
if let Some(exit_code) = output.status.code() {
if exit_code == 0 {
println!("Ok.");
} else {
eprintln!("Failed.");
}
} else {
eprintln!("Interrupted!");
}
println!("{}", String::from_utf8(output.stdout).unwrap());
return "Bruh".to_string()
}
The code outputs this:
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: FromUtf8Error { bytes: [255, 254, 13, 0, 10, 0, 72, 0, 75, 0, 76, 0, 77, 0, 92, 0, 83, 0, 121, 0, 115, 0, 116, 0, 101, 0, 109, 0, 92, 0, 67, 0, 117, 0, 114, 0, 114, 0, 101, 0, 110, 0, 116, 0, 67, 0, 111, 0, 110, 0, 116, 0, 114, 0, 111, 0, 108, 0, 83, 0, 101, 0, 116, 0, 92, 0, 67, 0, 111, 0, 110, 0, 116, 0, 114, 0, 111, 0, 108, 0, 92, 0, 84, 0, 101, 0, 114, 0, 109, 0, 105, 0, 110, 0, 97, 0, 108, 0, 32, 0, 83, 0, 101, 0, 114, 0, 118, 0, 101, 0, 114, 0, 92, 0, 87, 0, 100, 0, 115, 0, 92, 0, 114, 0, 100, 0, 112, 0, 119, 0, 100, 0, 92, 0, 83, 0, 116, 0, 97, 0, 114, 0, 116, 0, 117, 0, 112, 0, 80, 0, 114, 0, 111, 0, 103, 0, 114, 0, 97, 0, 109, 0, 115, 0, 13, 0, 10, 0, 32, 0, 32, 0, 32, 0, 114, 0, 100, 0, 112, 0, 99, 0, 108, 0, 105, 0, 112, 0, 13, 0, 10, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 114, 0, 100, 0, 112, 0, 99, 0, 108, 0, 105, 0, 112, 0, 13, 0, 10, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 82, 0, 68, 0, 80, 0, 32, 0, 67, 0, 108, 0, 105, 0, 112, 0, 98, 0, 111, 0, 97, 0, 114, 0, 100, 0, 32, 0, 77, 0, 111, 0, 110, 0, 105, 0, 116, 0, 111, 0, 114, 0, 13, 0, 10, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 77, 0, 105, 0, 99, 0, 114, 0, 111, 0, 115, 0, 111, 0, 102, 0, 116, 0, 32, 0, 67, 0, 111, 0, 114, 0, 112, 0, 111, 0, 114, 0, 97, 0, 116, 0, 105, 0, 111, 0, 110, 0, 13, 0, 10, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 49, 0, 48, 0, 46, 0, 48, 0, 46, 0, 49, 0, 57, 0, 48, 0, 52, 0, 49, 0, 46, 0, 55, 0, 52, 0, 54, 0, 13, 0, 10, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 99, 0, 58, 0, 92, 0, 119, 0, 105, 0, 110, 0, 100, 0, 111, 0, 119, 0, 115, 0, 92, 0, 115, 0, 121, 0, 115, 0, 116, 0, 101, 0, 109, 0, 51, 0, 50, 0, 92, 0, 114, 0, 100, 0, 112, 0, 99, 0, 108, 0, 105, 0, 112, 0, 46, 0, 101, 0, 120, 0, 101, 0, 13, 0, 10, 0, 32, 0, 32, 0, 32, 0, 32, 0, 32, 0, 50, 0, 48, 0, 48, 0, 55, 0, 48, 0, 49, 0, 50, 0, 54, 0, 45, 0, 48, 0, 50, 0, 48, 0, 48, 0, 51, 0, 56, 0, 13, 0, 10, 0, 13, 0, 10, 0, 72, 0, 75, 0, 76, 0, 77, 0, 92, 0, 83, 0, 79, 0, 70, 0, 84, 0, 87, 0, 65, 0, 82, 0, 69, 0, 92, 0, 77, 0, 105, 0, 99, 0, 114, 0, 111, 0, 115, 0, 111, 0, 102, 0, 116, 0, 92, 0, 87, 0, 105, 0, 110, 0, 100, 0, 111, 0, 119, 0, 115, 0, 32, 0, 78, 0, 84, 0, 92, 0, 67, 0, 117, 0, 114, 0, 114, 0, 101, 0, 110, 0, 116, 0, 86, 0, 101, 0, 114, 0, 115, 0, 105, 0, 111, 0, 110, 0, 92, 0, 87, 0, 105, 0, 110, 0, 108, 0, 111, 0, 103, 0, 111, 0, 110, 0, 92, 0, 85, 0, 115, 0, 101, 0, 114, 0, 105, 0, 110, 0, 105, 0, 116, 0, 13, 0, 10, 0, 32, 0, 32, 0, 32, 0, 67, 0, 58, 0, 92, 0, 87, 0, 105, 0, 110, 0, 100, 0, 111, 0, 119, 0, 115, 0, 92, 0, 115, 0, 121, 0, 115, 0, 116, 0, 101, 0, 109, 0, 51, 0, 50, 0, 92, 0, 117, 0, 115, 0, 101, 0, 114, 0, 105, 0, 110, 0, 105, 0
That is just a sample from the output. Seems like it is not valid utf8 or something idk I am really stumped on this one. I also tried with create_process_w but that had its own problems. Any help is appreciated, thanks!
Turns out, this is UTF-16. So you must first convert it from bytes into u16s:
let u16s: Vec<u16> = output.stdout.chunks_exact(2).map(|chunk| u16::from_ne_bytes([chunk[0], chunk[1]])).collect();
let s = String::from_utf16(&u16s).unwrap(); // may want to use from_utf16_lossy?
Also note that the output starts with a byte order mark: "\u{feff}\r\nHKLM". You may want to strip it off if it's present.
Ok so this is my final solution for my problem:
fn parse_utf16_bytes(bytes: &[u8]) -> Option<String> {
let mut chunks = bytes.chunks_exact(2);
let is_big_endian = match chunks.next() {
Some(&[254, 255]) => true,
Some(&[255, 254]) => false,
_ => return None,
};
let utf16: Vec<_> = chunks
.map(|x| {
let arr2 = x.try_into().expect("convert .chunks_exact() to [u8; 2]");
if is_big_endian {
u16::from_be_bytes(arr2)
} else {
u16::from_le_bytes(arr2)
}
})
.collect();
String::from_utf16(&utf16).ok()
}
Thanks for everybody that helped :)

AutoML prediction model PDF extraction: Required field not set

I'm trying to perform entity extraction on my prediction model but having issues passing PDF document from GCS.
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
const projectId = "id";
const location = "us-central1";
const modelId = "modelid";
const keyFilename = "./gcp.json";
// Imports the Google Cloud AutoML library
const { PredictionServiceClient } = require("#google-cloud/automl").v1;
// Instantiates a client
const client = new PredictionServiceClient({ keyFilename });
async function predict() {
// Construct request
const request = {
name: client.modelPath(projectId, location, modelId),
payload: {
document: {
input_config: {
gcs_source: {
input_uris: "gs://pathtofile.pdf",
},
},
},
},
};
const [response] = await client.predict(request);
for (const annotationPayload of response.payload) {
console.log(annotationPayload);
console.log(`Text Extract Entity Types: ${annotationPayload.displayName}`);
console.log(`Text Score: ${annotationPayload.textExtraction.score}`);
const textSegment = annotationPayload.textExtraction.textSegment;
console.log(`Text Extract Entity Content: ${textSegment.content}`);
console.log(`Text Start Offset: ${textSegment.startOffset}`);
console.log(`Text End Offset: ${textSegment.endOffset}`);
}
}
predict();
This gives the following error:
Error: 3 INVALID_ARGUMENT: List of found errors: 1.Field: payload.document.document_text.content; Message: Required field not set.
code: 3,
details: 'List of found errors:\t1.Field: payload.document.document_text.content; Message: Required field not set.\t',
metadata: Metadata {
internalRepr: Map(3) {
'grpc-server-stats-bin' => [
Buffer(10) [Uint8Array] [
0, 0, 183, 198, 95,
1, 0, 0, 0, 0
]
],
'google.rpc.badrequest-bin' => [
Buffer(67) [Uint8Array] [
10, 65, 10, 38, 112, 97, 121, 108, 111, 97, 100,
46, 100, 111, 99, 117, 109, 101, 110, 116, 46, 100,
111, 99, 117, 109, 101, 110, 116, 95, 116, 101, 120,
116, 46, 99, 111, 110, 116, 101, 110, 116, 18, 23,
82, 101, 113, 117, 105, 114, 101, 100, 32, 102, 105,
101, 108, 100, 32, 110, 111, 116, 32, 115, 101, 116,
46
]
],
'grpc-status-details-bin' => [
Buffer(222) [Uint8Array] [
8, 3, 18, 104, 76, 105, 115, 116, 32, 111, 102, 32,
102, 111, 117, 110, 100, 32, 101, 114, 114, 111, 114, 115,
58, 9, 49, 46, 70, 105, 101, 108, 100, 58, 32, 112,
97, 121, 108, 111, 97, 100, 46, 100, 111, 99, 117, 109,
101, 110, 116, 46, 100, 111, 99, 117, 109, 101, 110, 116,
95, 116, 101, 120, 116, 46, 99, 111, 110, 116, 101, 110,
116, 59, 32, 77, 101, 115, 115, 97, 103, 101, 58, 32,
82, 101, 113, 117, 105, 114, 101, 100, 32, 102, 105, 101,
108, 100, 32, 110,
... 122 more items
]
]
},
options: {}
},
statusDetails: [
BadRequest {
fieldViolations: [
FieldViolation {
field: 'payload.document.document_text.content',
description: 'Required field not set.'
}
]
}
]
}
I have checked all the passed parameters and they're fine.
I'm using this as an example: https://cloud.google.com/natural-language/automl/docs/predict#automl_language_entity_extraction_predict-nodejs
It works fine when I pass text but I would like to pass PDF as the payload.
Thank you

Node.js child_process issuewhile running in VS code

Below is the ERROR I am getting when I run following 2 lines of code
RUNNING IN VS CODE
ONLY 2 FOLLOWING LINES cause this trouble pls help me get started!!!!!!!!!
const cp = require("child_process");
cp.execSync("calc");
status: 127,
signal: null,
output: [
null,
Buffer(0) [Uint8Array] [],
Buffer(33) [Uint8Array] [
47, 98, 105, 110, 47, 115, 104, 58,
32, 99, 97, 108, 99, 58, 32, 99,
111, 109, 109, 97, 110, 100, 32, 110,
111, 116, 32, 102, 111, 117, 110, 100,
10
]
],
pid: 19417,
stdout: Buffer(0) [Uint8Array] [],
stderr: Buffer(33) [Uint8Array] [
47, 98, 105, 110, 47, 115, 104, 58,
32, 99, 97, 108, 99, 58, 32, 99,
111, 109, 109, 97, 110, 100, 32, 110,
111, 116, 32, 102, 111, 117, 110, 100,
10
]
}
Check if calc is installed and can be found. 127 indicates that the program is not found.

Post request not getting req.body

I am trying to make a post request with Postman. The only thing that returns is the id made by uuidv4() all the other information for the JSON object is not there.
I have tried req.body and req.apiGateway.event.body with no luck.
This is my post request:
const serverless = require("serverless-http");
const express = require("express");
const app = express();
// const bodyParser = require("body-parser");
const AWS = require("aws-sdk");
const db = new AWS.DynamoDB.DocumentClient();
const { v4: uuidv4 } = require("uuid");
app.use(express.urlencoded());
app.use(express.json());
// app.use(bodyParser.urlencoded({ extended: true }));
// app.unsubscribe(bodyParser.json());
app.post("/inspections", async (req, res) => {
const data = req.apiGateway.event.body;
const params = {
TableName: "inspectionTrackTable",
Item: {
id: uuidv4(),
unitNum: data.unitNum,
building: data.building,
managed: data.managed,
rental: data.rental,
inHouse: data.inHouse,
robeCount: data.robeCount,
inspected: data.inspected,
notes: data.notes
},
};
try {
await db.put(params).promise();
res.status(201).json({ unit: params.Item });
} catch (e) {
res.status(500).json({ error: e.message });
}
});
This is what I am entering in Postman:
{
"unitNum": "007",
"building": "A",
"managed": true,
"rental": false,
"inHouse": false,
"robCount": 0,
"inspected": false,
"notes": "this is a good unit"
}
I have now tried adding ...req.body to the item like this:
Item: {
id: uuidv4(),
...req.body
},
The response I am getting from that is:
{
"unit": {
"0": 123,
"1": 10,
"2": 32,
"3": 32,
"4": 32,
"5": 32,
"6": 34,
"7": 117,
"8": 110,
"9": 105,
"10": 116,
"11": 78,
"12": 117,
"13": 109,
"14": 34,
"15": 58,
"16": 32,
"17": 34,
"18": 48,
"19": 48,
"20": 55,
"21": 34,
"22": 44,
"23": 10,
"24": 32,
"25": 32,
"26": 32,
"27": 32,
"28": 34,
"29": 98,
"30": 117,
"31": 105,
"32": 108,
"33": 100,
"34": 105,
"35": 110,
"36": 103,
"37": 34,
"38": 58,
"39": 32,
"40": 34,
"41": 65,
"42": 34,
"43": 10,
"44": 32,
"45": 32,
"46": 32,
"47": 32,
"48": 10,
"49": 125,
"id": "efb90ad4-f68e-4ea0-b55c-6a4f79e3a96c"
}
}
I did not expect that...
My delete, and get requests are working I am having trouble with the put and post..

Data being modified during axios.get request

I am currently on a React project and the data being sent from the backend is being modified as it arrives on the frontend. I'm probably missing something really stupid but I cannot figure out where it's going bad for god's sake.
The frontend component
import React from 'react';
//.. import bunch of other things
const styles = theme => ({
root: {},
sectionTitle: {
margin: '20px 0 35px 0'
},
sectionTitleNoTop: {
margin: '0 0 35px 0'
},
gap50: {
margin: 50
}
});
class RankingsPage extends React.Component {
state = {
tableData: null,
tableDataEarnings: null,
barData: null,
barDataEarnings: null,
tableError: ''
};
constructor(props) {
super(props);
this.getBarData = this.getBarData.bind(this);
}
componentWillMount() {
this.getBarData(false); //first request
this.getBarData(true); //second request
}
async getBarData(earnings) {
let response = await axios.get(`/api/ranking/ranking_barchart?earnings=${earnings}`);
let {success, data, error} = response.data;
console.log(earnings);
console.log(data);
//The first request works fine. retrieves following
//[ { fill: true,
// backgroundColor: 'rgba(58, 79, 212, 0.4)',
// data: [ 11, 8, 8, 5, 5, 4, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] },
// { fill: true,
// backgroundColor: 'rgba(239, 49, 81, 0.4)',
// data: [ 0, 0, 0, 0, 10, 0, 15, 6, 10, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0 ] } ]
//Second request gives this
//[ { fill: true,
// backgroundColor: 'rgba(58, 79, 212, 0.4)',
// data: [ 11, 8, 8, 5, 5, 4, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] },
// { fill: true,
// backgroundColor: 'rgba(239, 49, 81, 0.4)',
// data: [ 11, 8, 8, 5, 5, 4, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] } ]
//Notice that this part is different from the first
if (!success) {
return this.setState({barDataError: error});
}
if (!earnings) {
return this.setState({barData: data});
}
this.setState({barDataEarnings: data});
}
render() {
const {classes} = this.props;
const {tableData, tableDataEarnings, barData, barDataEarnings} = this.state;
return (
<div className={classes.root}>
//I'm using this.state.barData and this.state.barDataEarnings here - I don't think this code has anything to do with the problem but if you need it I'll provide
</div>
);
}
}
export default withStyles(styles)(RankingsPage)
The component makes EXACTLY the same request except that the query value is different in the url. However, the data of the second index object is different to that of the first response.
Now the more interesting thing is the backend.
app.get('/api/ranking/ranking_barchart', authorize, async (req, res) => {
try{
let earnings = req.query.earnings === 'true';
// let sortBy = earnings ? {totalReceived: -1} : {totalDonated: -1};
let sortBy = {totalReceived: -1};
let topUsersResult =
await User.find({})
.sort(sortBy)
.limit(20)
.lean()
.exec();
let returnBody = {
labels: [],
datasets: []
};
let donationsMade = {
fill: true,
backgroundColor:'rgba(239, 49, 81, 0.4)',
data: []
};
let donationsReceived = {
fill: true,
backgroundColor:'rgba(58, 79, 212, 0.4)',
data: []
};
for(let i = 0; i< topUsersResult.length; i++) {
let curResult = topUsersResult[i];
returnBody.labels.push(curResult.username);
donationsMade.data.push(
curResult.totalDonated ? curResult.totalDonated : 0
);
donationsReceived.data.push(
curResult.totalReceived ? curResult.totalReceived : 0
);
}
returnBody.datasets.push(donationsReceived);
returnBody.datasets.push(donationsMade);
console.log(returnBody.datasets); //Print here is EXACTLY the same for both requests as expected
res.json(API_MSG.successMsg(returnBody));
}catch(e){
res.json(API_MSG.errorMsg(e, MSG_TYPES.FIND_ERROR, true));
}
});
I have it to NOT use the earnings query to test out with the EXACT same response. The code above is almost unnecessary because the console.log value is exactly the same as shown below. The console.log commented on the code above prints the following
//First Request
[ { fill: true,
backgroundColor: 'rgba(58, 79, 212, 0.4)',
data: [ 11, 8, 8, 5, 5, 4, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] },
{ fill: true,
backgroundColor: 'rgba(239, 49, 81, 0.4)',
data: [ 0, 0, 0, 0, 10, 0, 15, 6, 10, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0 ] } ]
//Second Request
[ { fill: true,
backgroundColor: 'rgba(58, 79, 212, 0.4)',
data: [ 11, 8, 8, 5, 5, 4, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] },
{ fill: true,
backgroundColor: 'rgba(239, 49, 81, 0.4)',
data: [ 0, 0, 0, 0, 10, 0, 15, 6, 10, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0 ] } ]
PLEASE tell me I'm not crazy. How is the EXACT same response that is being returned from the server some how being modified while it's being transported to the frontend???

Resources