Unexpected End of Input Error - javascript

I have this nodejs app and one of the calls I've made is an upsert call that upserts the request body to mongodb. If the request body exists in mongo, update it, else insert it. The insert portion works correctly, however, I am having trouble with the upsert portion. My request body is being generated by a C# app that I've written.
Ok, so when I update, I need to use the $addToSet and $each commands because I am adding elements into an array in the mongo document.
Here is an example request body (this is a POST), this is what's sent to my nodejs app:
{
"$addToSet": {
"Data": {
"$each": [
{"ID":"10","RandNum":"45"},
{"ID":"11","RandNum":"1"},
{"ID":"12","RandNum":"3"}
]
}
}
}
The document is formatted like this in mongo:
{
"Timestamp": "timestamp",
"Id": "12345",
"Header": {
"Name": "Name",
"Query": "SELECT * FROM test"
},
"Data": [
{
"ID": "1",
"RandNum": "34"
}, {
"ID": "4",
"RandNum": "23"
}
]
}
I am only interested in inserting into the "Data" array so I am using the $addToSet and $each operators.
This is my upsert call on the nodejs app, the actual function has a lot of code so I'll just put the upsert parts:
var db = require('../db.js');
var ind = require('./index.js');
var mongo = require('mongodb').MongoClient;
exports.upsert = function(req, res) {
var token = req.params.token;
var app = req.params.applicationId;
var dataObject = {
"Status": 1,
"DataObject": {},
"UserFriendlyErrorMessage": "",
"DeveloperErrorMessage": ""
};
// set response header
res.writeHead(200, {"Content-Type": "application/json"});
res.writeHead(200, {"Content-Length": Buffer.byteLength(req.body)});
var upsertDatabase = mongo.connect(ind.mongoLoc + databaseName, function(err, newDb) {
if(err)
throw err;
var upsertCollection = newDb.collection(collectionName);
if(newDb) {
if(req.query.query) {
// UPSERT
try {
var json = JSON.parse(req.query.query);
upsertCollection.update(json, req.body, {upsert: true, multi: false}, function(err, records) {
if(err)
throw err;
console.log("Data updated");
});
} catch(e) {
dataObject["DataObject"] = null;
dataObject["UserFriendlyErrorMessage"] = "Could not update";
dataObject["DeveloperErrorMessage"] = e;
res.end(JSON.stringify(dataObject, 0, 4));
console.log(e);
}
} else {
// INSERT
upsertCollection.insert(req.body, function(err, records) {
if(err)
throw err;
console.log("Data inserted");
});
}
dataObject["Status"] = 0;
dataObject["DataObject"] = null;
res.end(JSON.stringify(dataObject, 0, 4));
} else {
// could not connect to database
dataObject["DataObject"] = null;
dataObject["UserFriendlyErrorMessage"] = "Could not connect to database";
dataObject["DeveloperErrorMessage"] = "ERROR: Could not connect to database";
res.end(JSON.stringify(dataObject, 0, 4));
}
});
};
In this function, I am testing to see if the document exists and then upserting it if it does. That's the part I'm focusing on. The dataObject is what I'm returning to the user to show errors/successes.
When I post the data to this call, I get this error (I'm viewing the POST call in Fiddler):
HTTP/1.1 504 Fiddler - Receive Failure
Date: Fri, 28 Feb 2014 15:30:39 GMT
Content-Type: text/html; charset=UTF-8
Connection: close
Cache-Control: no-cache, must-revalidate
Timestamp: 10:30:39.535
[Fiddler] ReadResponse() failed: The server did not return a response for this request.Server returned 0 bytes
Content-Type is supposed to be application/json

I fixed it. It seems that the line
res.writeHead(200, {"Content-Length": Buffer.byteLength(req.body)});
was not allowing it to post the data. I tried removing it and the upsert worked.
However, I'm wondering why removing that did the trick. I thought that Content-Length was necessary because it specifies the length of the body.

Related

need Mongoose Model.find() to work when queries aren't present

I'm currently working on the freeCodeCamp Exercise Tracker Project on Replit.
my Project Link: https://replit.com/#mazorSharp/Exercise-Tracker?v=1
If you click on the link, the code I'm referring to is in the server.js file and It's the code under the comment labeled // NUMBER 3
I'm running into an issue with one of the GET routes.
GET user's exercise log: GET /api/users/:_id/logs?[from][&to][&limit]
The GET route works fine when all queries are used in the get search. Queries for the test are From, To, and Limit. If one of the queries aren't present in the GET request I get an error.
CastError: Cast to date failed for value "Invalid Date" (type Date) at path "date" for model "exerciseInfo"
What steps would I need to take to make sure if someone isn't putting in values for FROM, TO, and LIMIT queries that it wouldn't throw an error because of it?
app.get('/api/users/:_id/logs', (req, res) => {
const {from, to, limit} = req.query;
let idJson = {"id": req.params._id}
let idToCheck = idJson.id;
console.log("from=> ", from, "to=> ", to, "limit=> ", limit, "idToCheck=> ", idToCheck);
//Check ID
ExerciseInfo.findById(idToCheck, (err, data) => {
if (err) {
console.log("Error with ID => ", err);
} else {
// Find Username Documents
ExerciseInfo.find(({username: data.username}, {date: {$gte: new Date(from), $lte: new Date(to)}}), null , {limit: +limit} , (err, doc) => {
let loggedArray = []
if (err) {
console.log("error with username=> ", err);
} else {
console.log("all docs related to username=> ", doc);
let documents = doc;
let loggedArray = documents.map((item) => {
return {
"description": item.description,
"duration": item.duration,
"date": item.date.toDateString(),
}
})
const test = new LogInfo({
// "_id": idToCheck,
"username": data.username,
"from": from,
"to": to,
"count": limit,
"log": loggedArray,
})
test.save((err, data) => {
if (err) {
console.log(err);
} else {
console.log("saved exercise successfully")
res.json({
"_id": idToCheck,
"username": data.username,
"from": data.from.toDateString(),
"to": data.to.toDateString(),
"count": data.count,
"log": loggedArray,
})
}
})
}
})
}
})
})

How to batch insert data into MySQL in NodeJS?

I am developing a REST API. One of the end points I have recieve a list of data like below.
[
{
"iduser": 3,
"title": "House in kandala",
"description": "Built a house in kandala area"
},
{
"iduser": 3,
"title": "House in NYC",
"description": "Built a house in greater NYC area"
}
]
I need to save the list into the database. Below is my code.
const mysql = require('mysql2');
const errorCodes = require('source/error-codes');
const PropertiesReader = require('properties-reader');
const prop = PropertiesReader('properties.properties');
const con = mysql.createConnection({
host: prop.get('server.host'),
user: prop.get("server.username"),
password: prop.get("server.password"),
port: prop.get("server.port"),
database: prop.get("server.dbname")
});
exports.saveSellerPortfolioItem = (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;
if (event.body == null && event.body == undefined) {
var response = errorCodes.missing_parameters;
callback(null, response)
}
else {
let body = JSON.parse(event.body)
console.log("body", body);
let iduser = Number(body.iduser);
let title = body.title;
let description = body.description;
if (isNaN(iduser)) {
var response = errorCodes.invalid_parameter;
callback(null, response);
}
else {
// allows for using callbacks as finish/error-handlers
const sql = "INSERT INTO seller_portfolio_item (iduser, title, description) VALUES (?,?,?)";
con.execute(sql, [iduser, title, description], function (err, result) {
if (err) {
console.log(err.toString());
if (err.toString().indexOf('cannot be null') >= 0) {
var response = errorCodes.not_null_parameters;
callback(null, response);
}
var response = errorCodes.internal_server_error;
callback(null, response);
}
else {
var response = {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": JSON.stringify({ insertId: result.insertId }),
"isBase64Encoded": false
};
callback(null, response)
}
});
}
}
};
My code is capable of inserting just one record, not suitable to save multiple when I am sending a list. As a result, client program will have to call the same method again and again in a loop.
How can I read the list and insert multiple records ?
You are correct that going forward it is better to use mysql instead of mysql2. Below is one approach that can be used to batch insert multiple records.
Be sure to run npm install mysql --save to ensure you have to necessary package installed.
Working with multiple records requires some additional thinking and planning as well. You should consider:
does your table contain any unique keys other than the primary?
is it possible your API function will ever attempt to insert a duplicate?
in the event of a duplicate how should it be handled?
do you need to know the insert ID for every new record created?
will every object in your list always have the same number of entries, the same keys, and expected values?
Depending on your answers to the above considerations the example I provided below would require additional code and complications. This example is the simplest implementation of the idea.
// package changed, remember to npm install…
const mysql = require('mysql');
const errorCodes = require('source/error-codes');
const PropertiesReader = require('properties-reader');
const prop = PropertiesReader('properties.properties');
const con = mysql.createPool({
connectionLimit: 10,
host: prop.get('server.host') || '127.0.0.1',
user: prop.get("server.username") || 'local_user',
password: prop.get("server.password") || 'local_password',
database: prop.get("server.dbname") || 'local_database',
multipleStatements: true, // necessary to run chained queries
charset: 'utf8mb4' // necessary if you might need support for emoji characters - table charset must match
});
exports.saveSellerPortfolioItem = (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;
// It is better to check for the existence of your
// expected request body in the controller stage of
// your app but I've included this for consistency
// with your original code.
let query_object = event.body ? JSON.parse(event.body) : null;
console.log('query_object', query_object);
if (!query_object.length) {
let response = errorCodes.missing_parameters;
callback(null, response)
}
else {
// use the keys of the first object to define the field names.
// you don't have to use this approach but it provides flexibility
// if you will not always use the same fields
let keys = Object.keys(query_object[0]);
// map the values into a supported format
let values = query_object.map( obj => keys.map( key => obj[key]));
let sql = 'INSERT INTO seller_portfolio_item (' + keys.join(',') + ') ?;'
con.query(sql, values, function(error, results, fields) {
if (error) callback(null, error);
// when inserting multiples you will only get back the
// insert id of the first record. if there are updates
// due to duplicate keys, you won't even get that.
// results will look like this:
console.log(results);
// Expected output
// OkPacket {
// fieldCount: 0,
// affectedRows: 3,
// insertId: 1,
// serverStatus: 2,
// warningCount: 6,
// message: '&Records: 3 Duplicates: 0 Warnings: 6',
// protocol41: true,
// changedRows: 0
// }
let response = {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": JSON.stringify({ records_inserted: results.affectedRows }),
"isBase64Encoded": false
};
callback(null, response)
});
}
};

Why does MongoDB update always result in error response

Why is a successful update response always passed as an error. is something wrong with my code or is it the Mongo DB response.
However I don't see any errors when I check them on MongoDB.
MongoDB Enterprise > db.getLastError();
null
Response:
{
"lastErrorObject": {
"n": 1,
"updatedExisting": true
},
"value": {
"_id": "1111111111",
"xyz": "hgfjdfjk"
}
}
Code Block
{
try {
const db = client.db(dbName);
db.collection(cName).updateOne({ _id: id }, { $pull: query }).then((err, result) => {
if (err) {
callBack(err);
} else {
callBack(null, result);
}
});
client.close();
}
catch (err) {
callBack({
error: 'Unable to process the request',
errorMessage: err
})
}
}
getLastError() returns the server response to the current operation within that connection, and is not related to an actual error in MongoDB. It is part of MongoDB's wire protocol.
See getLastError for more details.

node js:Cannot read property 'text' of undefined

I am bulding a messenger bot with node js express.I am trying to split my index.js file into two files. Here is the code for msg.js which is the new file
'const
express = require('express'),
bodyParser = require('body-parser'),
request = require('request'),
PAGE_ACCESS_TOKEN ="",
app = express().use(bodyParser.json());
//functions
module.exports = {
//hangles messages
handleMessage:function (sender_psid, received_message) {
let response;
// Checks if the message contains text
if (received_message.text) {
// Create the payload for a basic text message, which
// will be added to the body of our request to the Send API
response = {
"text": `You sent the message: "${received_message.text}". Now send me an attachment!`
}
} else if (received_message.attachments) {
// Get the URL of the message attachment
let attachment_url = received_message.attachments[0].payload.url;
response = {
"attachment": {
"type": "template",
"payload": {
"template_type": "generic",
"elements": [{
"title": "Is this the right picture?",
"subtitle": "Tap a button to answer.",
"image_url": attachment_url,
"buttons": [
{
"type": "postback",
"title": "Yes!",
"payload": "yes",
},
{
"type": "postback",
"title": "No!",
"payload": "no",
}
],
}]
}
}
}
}
// Send the response message
module.exports.callSendAPI(sender_psid, response);
},
// Handles messaging_postbacks events
handlePostback:function (sender_psid, received_postback) {
let response;
// Get the payload for the postback
if (received_postback) {
let payload = received_postback.payload;
}
// Send the message to acknowledge the postback
module.exports.callSendAPI(sender_psid, response);
},
// Sends response messages via the Send API
callSendAPI:function (sender_psid, response) {
// Construct the message body
let request_body = {
"recipient": {
"id": sender_psid
},
"message": response
}
// Send the HTTP request to the Messenger Platform
request({
"uri": "https://graph.facebook.com/v2.6/me/messages",
"qs": { "access_token": PAGE_ACCESS_TOKEN },
"method": "POST",
"json": request_body
}, (err, res, body) => {
if (!err) {
console.log('message sent!')
} else {
console.error("Unable to send message:" + err);
}
});
}
};
I have the following code at the bottom of my index.js file.
//Imports functions from other files
let msg = require('./msg.js'),
handleMessage = msg.handleMessage(),
handlePostback = msg.handlePostback(),
callSendAPI = msg.callSendAPI();
I am getting the following error:
msg.js:14
if (received_message.text) {
^
TypeError: Cannot read property 'text' of undefined
The problem is this line:
if (received_message.text) {
When this gets called, received_message which is passed in is undefined, so when you try to get the text field from the received_message variable it will throw an error since received_message is undefined and thus would not have any fields you could call from it. Check to see if received_message is ever set properly before being passed into your handleMessage function.

Post request returns undefined on the server side

So, I have this POST request I made
$("#pacotes").on('click', ".produto", function () {
console.log(this.id);
$.post("http://localhost:3000/pacote?idPacote=" + this.id);
});
The log returns a number on the client side, as it should.
The post then goes through my route and arrives here
exports.Pacote = function (req, res) {
console.log("gato");
var pacote = req.idPacote;
console.log(pacote);
connection.connection();
global.connection.query('SELECT * FROM Pacote WHERE idPacotes = ? LIMIT 1', [pacote], function (err, result) {
if (result.length > 0) {
if (result) {
var object = JSON.parse(JSON.stringify(result));
var packObject = object[0];
if (result.length > 0) {
if (result) {
res.render('home', { title: 'pacote', layout: 'pacote', data: packObject });
}
}
} else if (err) {
console.log(err);
}
};
});
}
The first log is just a flag to see if it is reaching the point, which it is
But the second log should return a number, yet it is returning undefined
I'm not very experienced in this subject, but this has always worked for me.
I don't understand where I went differently as my login function is nearly the same thing and returns actual values as expected. Maybe because of bodyparser, but I dont know.
It just bothers me that the id returns properly on the client side but as undefined on the server side
I also tried the same thing but with GET and the results didnt change
You are passing "idPacote" in query string. You will get the the query string parameters in "req.query" if you are using Express with NodeJS. Try this
var pacote = req.query.idPacote;
instead of
var pacote = req.idPacote;
The var pacote = req.idPacote; should be replaced with (provided that you send it as GET parameter):
var pacote = req.params.idPacote;
A side note: you should be using connection pooling in order to improve performance in your app, for example:
var mysql = require("mysql");
//Database connection parameters
var config = {
connectionLimit: 10000,
host: "127.0.0.1",
user: "user",
password: "password",
database: "database",
charset: "utf8_general_ci",
connectTimeout: 4000
};
//Pool
var pool = mysql.createPool(config);
function connection(){
//Assign connection pool for further reuse
this.init = function () {
this.pool = pool;
};
//Get connection
this.acquire = function(callback){
this.pool.getConnection(function(error, con){
if (error) {
if (this.pool)
//Close all connections in pool
this.pool.end(function(err){});
console.log("\x1b[31m" + error, "\x1b[0m");
}
else {
callback(error, con);
}
});
};
}
Read more here.

Categories