Im doing a RESTful server on NodeJS and MongoDB but im stuck on the DELETE method, because im getting the error
"Argument passed in must be a single String of …modules\express\lib\router\index.js:174:3"
when trying to cast the req.body.listId into an ObjectId.
Here's my code:
router.delete('/', function(req, res){
var db = req.db;
var collection = db.get('listcollection');
var oId = new ObjectId(req.body.listId); //The exception is here
collection.remove(
{
"_id": oId
},function(err,doc){
if (err) {
res.json("There was a problem deleting the information to the database.");
}
else {
res.json("Successfully deleted");
}
}
);
});
Solved!:
The listId parameter was quoted ("58f8b2cc8cf726ca76551589") so I did an slice. Anyway I changed the param to be received in the URL, here's the code: Thanks!!
router.delete('/:listId', function(req, res){
var db = req.db;
var collection = db.get('listcollection');
var listId = req.params.listId;
listId = listId.slice(1, listId.length - 1);
var oId = new ObjectId(listId);
collection.remove(
{
"_id": oId
},function(err,doc){
if (err) {
res.json("There was a problem deleting the information to the database.");
}
else {
res.json("Successfully deleted");
}
}
);
});
The error happens because req.body.listId does not follow the rule of ObjectId -- ObjectId is a string of 12 bytes (24 0-9 or a-f characters, e.g. "507f1f77bcf86cd799439011"). What may go wrong is:
req.body.listId is a plain number, not a String type, e.g. 89876.
req.body.listId is a String, but not follow the rule described above.
Although it is not RESTful, HTTP DELETE request with JSON body is totally OK. The Node.js server can receive the request and its body.
Related
I'm trying to recieve data from a MQTT node which I then want to proceed with putting into a MYSQL database. From what I've understood I need to use Javascript to do this, I however can't find any examples of this which will work. Is there anyone who have done this before who could help out? This specifically is about how to make a script in Javascript to send the information from the MQTT broker to a MYSQL database in node red. The question that was suggested as an answer is not specifically for Node Red nor does it offer any answers to my question about using Javascript as a way to achieve what I'm trying to do. The answer to that question was to use Node red but it was to no help with how you should use it.
yeah, you can use any language for sending payload from MQTT to MYSQL.
basically what you can do is set a small node which will subscribe to all the incoming payload and dump it in your MYSQL Db
here is the JS script:-
var mqtt = require('mqtt'); //https://www.npmjs.com/package/mqtt
var Topic = '#'; //subscribe to all topics
var Broker_URL = 'mqtt://MQTT_BROKER_URL';
var Database_URL = 'Database_URL';
var options = {
clientId: 'MyMQTT',
port: 1883,
keepalive : 60
};
var client = mqtt.connect(Broker_URL, options);
client.on('connect', mqtt_connect);
client.on('reconnect', mqtt_reconnect);
client.on('message', mqtt_messsageReceived);
client.on('close', mqtt_close);
function mqtt_connect() {
console.log("Connecting MQTT");
client.subscribe(Topic, mqtt_subscribe);
};
function mqtt_subscribe(err, granted) {
console.log("Subscribed to " + Topic);
if (err) {console.log(err);}
};
function mqtt_reconnect(err) {
console.log("Reconnect MQTT");
if (err) {console.log(err);}
client = mqtt.connect(Broker_URL, options);
};
function after_publish() {
//do nothing
};
//receive a message from MQTT broker
function mqtt_messsageReceived(topic, message, packet) {
var message_str = message.toString(); //convert byte array to string
console.log("message to string", message_str);
message_str = message_str.replace(/\n$/, ''); //remove new line
//message_str = message_str.toString().split("|");
console.log("message to params array",message_str);
//payload syntax: clientID,topic,message
if (message_str.length == 0) {
console.log("Invalid payload");
} else {
insert_message(topic, message_str, packet);
//console.log(message_arr);
}
};
function mqtt_close() {
//console.log("Close MQTT");
};
////////////////////////////////////////////////////
///////////////////// MYSQL ////////////////////////
////////////////////////////////////////////////////
var mysql = require('mysql'); //https://www.npmjs.com/package/mysql
//Create Connection
var connection = mysql.createConnection({
host: Database_URL,
user: "newuser", //DB Username
password: "mypassword", //DB Password
database: "mydb" //DB Name
});
connection.connect(function(err) {
if (err) throw err;
//console.log("Database Connected!");
});
//insert a row into the tbl_messages table
function insert_message(topic, message_str, packet) {
var message_arr = extract_string(message_str); //split a string into an array
var clientID= message_arr[0];
var message = message_arr[1];
var date= new Date();
var sql = "INSERT INTO ?? (??,??,??,??) VALUES (?,?,?,?)";
var params = ['tbl_messages', 'clientID', 'topic', 'message','date', clientID, topic, message, date];
sql = mysql.format(sql, params);
connection.query(sql, function (error, results) {
if (error) throw error;
console.log("Message added: " + message_str);
});
};
//split a string into an array of substrings
function extract_string(message_str) {
var message_arr = message_str.split(","); //convert to array
return message_arr;
};
//count number of delimiters in a string
var delimiter = ",";
function countInstances(message_str) {
var substrings = message_str.split(delimiter);
return substrings.length - 1;
};
Reference:-
https://github.com/karan6190/MQTT-DB-plugin/blob/master/mqttTOmysql.js
You can use any language to send messages from MQTT to MySQL database(or any other).
For example, you can create a separate python service which uses Paho MQTT client and subscribes to all the topics and adds that data to a database when the message is received.
Here is how the code will look like in Python:
def on_message(client, userdata, msg):
topic = msg.topic
payload = msg.payload
# run mysql query using library like MySQLdb
# https://www.tutorialspoint.com/python/python_database_access.htm
topic = '#" #subscribe to all topics
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.subscribe(topic)
client.connect(mqttserver)
client.loop_forever()
I am trying to have a simple post function which uses bcrypt on the password passed, then stores the data in the database, however when I call the post request in postman I am getting an error. I add a console output to see what the body was showing up as, and it is just showing an empty object. Does anyone know what could be causing this? Below is my server code:
app.post('/createUser/', function(req, res) {
console.log("below is the req body");
console.log(req.body);
var pass = bcrypt.hashSync(req.body.password, salt);
var createPromise = interact.createUser(req.body.username,
pass,);
//did promise
createPromise.then(function(createResponse) {
if (createResponse.length > 0){
//this means that there was a user with that username in the db
res.json("yes");
}
else {
// otherwise, there wasn't anything in the database with this id
res.json("no");
}
}).catch(function(err) {
console.log(err);
console.log(req.body);
res.status(500).json(err);
});
});
I'm trying to make a simple task.
In the first place, on client side, i'm sending data to server and then i insert these data into my mongodb database.
Then i try to get count of clients from my database.
var express = require('express');
var MONGO_URL = "mongodb://localhost:27017/mydatabase";
var app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server),
mongo = require('mongodb').MongoClient,
fs = require('fs');
var countUserSuscribed =0;
//here i insert data
/* Connection events */
io.on('connection', function (client) {
console.log("User connected");
client.on('InsertNewUser', function (newUser) {
console.log("we ar in InsertNewUser event");
//////////////////////////////////////////////////////////
mongo.connect(MONGO_URL, function (err, db) {
console.log("we are connected to mongodb");
var Users = db.collection('User');
console.log("on crée la collection et on fait l'ajout");
Users.insert({ player: myP }, function (err, o) {
if (err) { console.warn(err.message); }
else { console.log("user inserted into db: user"); }
});
});
})
});
//GET COUNT USER
console.log("here we get count user");
mongo.connect(MONGO_URL, function (err, db) {
countUserSuscribed = Users.count();
console.log("we got " + countUserSuscribed + " user in mongoDB");
});
With this code i can create collections and insert documents but the count function doesn't work and i didn't find much explanations on npm documentation.
Is it possible to use others mongodb functions than insert and collection with socket.io-mongodb ?
If it is, can someone give an example or explain me how to use it?
The count function works but is async function and takes a callback.
here's the fix:
countUserSuscribed = Users.count(function (err,c) { console.log(c) });
https://www.npmjs.com/package/mongodb-autoincrement consider using that. It keeps a track of all inserted document. Plus it has a handy feature to get the next count. Example let's say you inserted two records. If you call next count it will show 3. There fore to get the total documents inserted call get next count - 1. Make sense?
Sorry here is the correct one. https://www.npmjs.com/package/mongoose-auto-increment
I am converting my database connection to MongoClient and am having difficulty changing parts of my previous code over.
Previously, in order to retrieve all documents from a collection, I would use:
$.getJSON('/users/infolist', function(data) {
$.each(data, function(){
//cycles through each document and do whatever
});
});
which would call the following:
router.get('/infolist', function(req, res) {
var db = req.db;
var collection = db.get('empcollection');
collection.find({$query: {}, $orderby: {age:1}},{},function(e,docs){
res.json(docs);
});
});
After looking at documentation online, I still have not figured out how to replicate this behavior with MongoClient. I have established connection, and can query the database, but returning the collection, and cycling through each document as done above is not working.
Any advice or help would be greatly appreciated.
From your explanation, I understand that you want to use the native mongodb driver to retrieve a list of documents from your collection, update them using a loop, and then retrieving them to the client:
var MongoClient = require('mongodb').MongoClient;
//Establish a connection to your database
MongoClient.connect('mongodb://your/connection/string', function(err, db) {
if(err) {
console.log('There was an error connecting to the database');
}
//Map empcollection to a variable
var collection = db.collection('empcollection');
//Query the collection and retrieve the documents in an array
collection.find({}).toArray(function(err, docs)) {
if(err) {
console.log('There was an error retrieveing the matched documents');
}
//Loop through the documents and change them accordingly
for(var i = 0; i < docs.length; i++) {
//........
}
//Retrieve the updated data
res.json(docs);
}
});
});
I am using MongoDB native driver for NodeJS, and am having trouble converting ObjectID to a string.
My code looks like:
db.collection('user', function(err, collection) {
collection.insert(data, {safe:true}, function(err, result) {
var myid = result._id.toString();
console.log(myid);
)};
});
I have tried various suggestions on StackOverflow like:
myid = result._id.toString();
myid = result._id.toHexString();
but none of them seemed to work.
I am trying to convert the ObjectID to base64 encoding.
Not sure if I am running into supported functionality under the Mongo native driver.
This work for me:
var ObjectID = require('mongodb').ObjectID;
var idString = '4e4e1638c85e808431000003';
var idObj = new ObjectID(idString);
console.log(idObj);
console.log(idObj.toString());
console.log(idObj.toHexString());
Output:
4e4e1638c85e808431000003
4e4e1638c85e808431000003
4e4e1638c85e808431000003
insert returns an array of results (as you can also send an array of objects to be inserted), so your code is trying to get the _id from the array instance rather than the first result:
MongoClient.connect("mongodb://localhost:27017/testdb", function(err, db) {
db.collection("user").insert({name:'wiredprairie'}, function(err, result) {
if (result && result.length > 0) {
var myid = result[0]._id.toString();
console.log(myid);
}
});
});
Also, you won't need to base64 encode the result of calling toString on an ObjectId as it's returned as a hex number already. You could also call: result[0]._id.toHexString() to get the Hex value directly (toString just wraps toHexString).