i am stacking on this problem since a week, it's a problem of synchronize on Node JS.
The process that I want to do is :
1- check the existence of table (collection). --> if not insertion of data
2- if the table was created, then i have to find all data on table and compare it with the data that i want to insert.
3- if the new data is already exist on the database (table) the program doesn't do any thing, if not the program inserts the new data to the the database (table).
So we have 3 functions should be scheduled.
function 1
var getCollection = function(collection, new_theme, nbr_indicateur,callback) {
dbObject.listCollections().toArray(function(err, collections){
if ( err ) throw err;
assert.equal(err, null);
collections.forEach(function(collect){
if(collect.name == collection)
{
callback(true);
}
else {
dbObject.collection(collection).insertOne( {
"name_theme" : new_theme,
"nbr_indicateur" : nbr_indicateur
}, function(err, result) {
assert.equal(err, null);
console.log("Inserted a document into the Table_Mapping_Theme collection.");
});
callback(false);
}
});
});
};
function 2 :
var getData = function(value, collection, theme, callback) {
var clb = true;
if(value)
{
dbObject.collection(collection).find({}).toArray(function(err, docs){
if ( err ) throw err;
assert.equal(err, null);
docs.forEach(function(doc){
if(doc.name_theme == theme)
{
console.log("ce theme existe déja");
clb = false;
}
});
});
}
callback(clb);
};
function 3 :
var insertData = function(value, collection, new_theme, nbr_indicateur, callback) {
if(value)
{
dbObject.collection(collection).insertOne( {
"name_theme" : new_theme,
"nbr_indicateur" : nbr_indicateur
}, function(err, result) {
assert.equal(err, null);
console.log("Inserted a document into the "+collection+" collection.");
});
}
callback("done");
};
calling those functions (app.post using express js)
here i tried with pyramid method but it doesn't work
app.post('/setting/add_theme', urlencodedParser, function(req, res) {
getCollection('Table_Theme', req.body.new_theme, req.body.nbr_indicateur, function(value0){ console.log("0"+value0);
getData(value0,'Table_Theme', req.body.new_theme, function(value1) { console.log("1"+value1);
insertData(value1, 'Table_Theme', req.body.new_theme, req.body.nbr_indicateur, function(value2){ console.log("2"+value2);
});
});
});
res.redirect('/setting');
});
Related
I have recently been desgining a back end web server that is primarily used to manipulate data from a mysql database. The server is designed in a DOM format, with a controller folder and an app.js
I have implemented a few GET, POST PUT requests in such a format
This is what one GET request looks like
app.get("/movies/:movieID/", (req, res, next) => {
const movieID = parseInt(req.params.movieID);
// if userID is not a number, send a 400.
if (isNaN(movieID)) {
res.status(400).send();
console.log("Error has occured with getting movieID")
return;
}
movies.findByID(movieID, (error, movie) => {
if (error) {
res.status(500).send();
console.log("get Id has error")
return;
};
if (movie === null) {
res.status(404).send();
console.log("movie ID is invalid")
return;
};
//console.log(movie)
res.status(200).send(movie);
console.log("Successful movie ID")
});
});
This is inside the controller folder
,findByID: function (movieID, callback) {
var dbConn = db.getConnection();
const findMovieByIDQuery = "select * from movies left join genres on movies.genreid = genres.genreid left join reviews on reviews.movieid = movies.movieid where movies.movieid = ?;";
dbConn.connect(function (err) {
if (err) {
console.log(err);
return callback(err, null);
} else {
dbConn.query(findMovieByIDQuery, [movieID], (error, results) => {
dbConn.end();
if(error) {
return callback(error, null);
}
else if (results.length === 0) {
callback(null, null);
return;
};
console.log(results)
return callback(null, results[0]);
});
}
});
}
How ive implemented it is to take the users input, in this case a number and input it in the query based on the ? position.
However, I am now trying to create on whereby the user is able to search based on a string not just an ID number.
This is my code so far
app.get("/movies/:movieKeyword", (req, res, next) => {
console.log("Reached the app.js")
movies.findByKeyword((error, moviesAvailable) => {
if (error) {
console.log(error);
res.status(500).send();
};
res.status(200).send(moviesAvailable);
});
});
,findByKeyword: function(callback) {
console.log("Reached find by keyword")
var dbConn = db.getConnection();
const findAllMoviesQuery = "SELECT title, description, cast, time, opening_date, picture from movies WHERE title LIKE '% ? %';"
dbConn.connect(function (err){
dbConn.query(findAllMoviesQuery, (error, results) => {
if (error) {
return callback(error, null);
};
console.log(results)
return callback(null, results);
});
});
}
To use the LIKE query in mysql, I need the variable to be stored in this format: "% ? %"
However, I am not able to get this query to work as the program is not able to insert the variable into the ? this time as it is within 2 quotation marks and 2 percentage symbols
I think you can use Template Literals. Here is a resource, Template Literals. You can remove the percentage signs and replace them with this syntax ${variable_name}
I have to insert multiple different JSON objects in MongoDB and then check whether the some of the data already exist in the database and run another query based on whether the data exists or not for each JSON Object. How can I do in expressjs? I am using mongojs package for working with MongoDB. The code I typed is below:
app.post('/addcard/:id', function(req, res) {
console.log("Received Add Card Request");
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth();
var day = date.getDate();
var yrval = req.body.yrval;
var monval = req.body.monval;
var dateval = req.body.dateval;
for (var i=0;i<req.body.phone.length;i++){
//console.log(i);
var card = new Card({
cardType : req.body.cardtype,
cardTitle : req.body.cardtitle,
allowMultipleStore : false,
phoneNumber : req.body.phone[i],
messageUser : req.body.message,
expiryDate : new Date(year+yrval,month+monval,day+dateval),
creditPoints : req.body.creditpoints,
punchCount : req.body.punch,
messageReachPunchLimit : req.body.limitmessage,
merchantUsersId : mongoose.Types.ObjectId(req.body.merchantuserid),
merchantId : mongoose.Types.ObjectId(req.params.id)
});
console.log(card);
db.carddata.insert(card, function (err,docInserted){
// console.log(card);
console.log(i);
if (err) throw err;
db.userdata.find({phoneNumber:req.body.phone},function (err,docs){
console.log("hiss");
if (err) throw err;
if (docs.length!=0){
var carduser = new CardUsersAssignment({
cardId : docInserted._id,
userId : docs[0]._id,
remainingCreditPoints : req.body.creditpoints,
remainingPunchCount : req.body.punch
});
db.carduser.insert(carduser,function (err){
console.log(" Card Details saved successfully_existing");
//console.log(i);
})
}//If (docs.length!=0)
else{
console.log(" Card Details saved successfully");
}
})//Finding by PhoneNumber
console.log(i+1);
})//Insert Function
console.log("hi");
} // End of For Loop
res.json({
success:true,
message:"Hello. You did it!"
});
});
This code is written as if I were writing for sequential execution. I know that NodeJS is asynchronous. I tried async.waterfall but it is giving error with the mongodb query function. Any help would be great. I am a NodeJS noob. Links to article which discuss similar scenarios would also be great.
You can achieve this using async library.
There is two way to do it.
Use async each to iterate your data and inside each check data is first check data is already exist or not, based on find result you can return or insert the doc.
It is the same as 1st, the only different is you just can to use waterfall for find and insert.
First Approach:
async.each(req.body.phone, function(data, callback) {
// Create card Info
db.carddata.insert(card, function (err,docInserted){
if (err) {throw err;}
db.userdata.find({phoneNumber:req.body.phone},function (err,docs){
if (err) {throw err;
} else if ( docs.length ){
// create carduser data
db.carduser.insert(carduser,function (err){
if (err) {throw err;}
callback();
}
} else {
console.log(" Card Details saved successfully");
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');
}
});
Second Approach:
async.each(req.body.phone, function(data, callback) {
//create card data
let data = {}
data.phone = req.body.phone;
data.docInserted = data.docInserted;
data.cardata = cardData;
async.waterfall([
insertCard,
updateDataFind,
cardDataInsert,
async.apply('insertCard', data)
], function (err, result) {
if(err){
if(err.success){
callback();
}
throw err;
}
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');
}
});
function insertCard(data, callback){
db.carddata.insert(card, function (err,data.docInserted){
if(err){throw err;}
callback(null, data);
}
}
function updateDataFind(data, callback){
db.userdata.find({phoneNumber:data.phone},function (err,docs){
if (err) {throw err;}
else if (docs.length!=0){ callback(null, data); }
else { callback({success:true}) }
}
}
function cardDataInsert(data, callback){
// create card user or pass from data.
db.carduser.insert(carduser,function (err){
if (err) {throw err;}
callback(null, data);
}
}
I am creating a service in sails js. I want to update the value of totalCount before returning it. But the problem is when the return is in the callback of the async.series I am getting an undefined when I'm invoking it. How should I do this?
var totalCount = 0;
async.series([
function getProducts(cb_series){
Inventory.find({sku_id : sku_id, bay_id : bay_id})
.then(function(inventory_items){
async.each(inventory_items, function(item, cb_each){
totalCount = totalCount + item.physical_count;
cb_each();
}, function(err){
if(err)
console.log(err);
cb_series();
});
});
}
], function returnResult(err, cb){
if(err)
console.log(err);
return totalCount;
});
I'm not totally sure what you're trying to do. But you probably want to pass totalCount out in a callback like this:
function getProducts(callback){
Inventory.find({sku_id : sku_id, bay_id : bay_id}).then(
function(inventory_items){
callback(null, inventory_items.length)
}, function(err){
console.log(err);
callback(err);
});
}
If there is an error, it will call back with the error as it's first parameter, so do a null check on that. If the first parameter is null, then the second parameter will be the length of your array.
If you'd rather return all of the products and not just the length (as the name of the function implies), then it's very similar:
function getProducts(callback){
Inventory.find({sku_id : sku_id, bay_id : bay_id}).then(
function(inventory_items){
callback(null, inventory_items)
}, function(err){
console.log(err);
callback(err);
});
}
You'd use it like this for the first case:
getProducts(function(err, productCount) {
if(err) {
console.log(err);
return err;
} else {
var totalCount = productCount;
}
//etc etc...
}
...or this for the second case:
getProducts(function(err,products) {
if(err) {
console.log(err);
return err;
} else {
var productArray = products;
}
//etc etc...
}
I'm new to expressJS and i'm wondering what is the best way to requery the database (mongo in my case) to get all the records after one is added.
exports.get = function (db) {
return function (req, res) {
var collection = db.get('notes');
collection.find({}, {}, function (e, docs) {
res.send(docs);
});
};
};
exports.create = function (db) {
return function (req, res) {
var title = req.body.title;
var note = req.body.note;
var collection = db.get('notes');
// Insert/update the note
collection.insert(
{
"title": title,
"note": note
},
function (err, doc) {
// If it failed, return error
if (err) {
res.send("There was a problem adding the information to the database. Error: "+err);
} else {
//res.redirect('/');
//res.json(db.get('notes'));
// WHAT IS THE BEST THING TO DO HERE TO GET ALL THE RECORDS INCLUDING THE ONE I'VE JUST ADDED?
exports.get(db);
}
}
);
}
};
I would replace
exports.get(db);
for
collection.find({}, {}, function (e, docs) {
res.send(docs);
});
The reason is that you are invoking this in the callback, AFTER the record has been inserted
Your exports.get function return a function, a kind of middleware I see.
Repplace
exports.get(db);
by
exports.get(db)();
I am new to node, and also JavaScript callbacks.
I am trying to check if an account exists in mongo and then 'save it' if it doesn't and return an error if it does.
I am currently trying to figure this out outside of my express app. This is what i have..
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://localhost:27017/main', function (err, db) {
if(err) throw err;
var query = { name : "www.website.com"}
findOne(db, query, function (doc) {
if(doc) {
console.log('account exists');
} else {
console.log('good to go');
}
console.dir(doc);
});
});
var findOne = function (db, query, callback) {
db.collection('accounts').findOne(query, function (err, doc) {
if(err) throw err;
db.close();
callback();
});
}
with the console.dir(doc); above returning as undefined. How do I wait for the findOne to return before using the callback to console.log or save the account?
The reason you are getting undefined is because when you call your callback your are not passing it the doc. That line should look like callback(doc).
Here is an updated version of your code with a few suggestions:
MongoClient.connect('mongodb://localhost:27017/main', function (err, db) {
if(err) throw err;
var query = { name : "www.website.com"}
findOne(db, query, function (err, doc) {
if(err) {
// something went wrong
console.log(err);
return;
}
if(doc) {
console.log('account exists');
console.dir(doc);
} else {
console.log('good to go');
}
});
});
var findOne = function (db, query, callback) {
db.collection('accounts').findOne(query, function (err, doc) {
db.close();
if(err) {
// don't use throw when in async code
// the convention is to call your callback with the error
// as the first argument (notice that I added an argument
// to the definition of your callback above)
callback(err);
}
else {
// call your callback with no error and the data
callback(null, doc);
}
});
}