I want to get the data from oraclde db using nodejs and disply it over angularjs based ui. PFB my code for service.js :
var async = require('async');
var oracledb = require('oracledb');
var dbConfig = require('../utility/dbconfig.js');
var response = require('../utility/response.js');
var bodyParser = require('body-parser');
var express = require('express');
var router = express.Router();
router.use(bodyParser.json());
var dbdata='';
var app = express();
var oracledb = require('oracledb');
var dbConfig = require('../utility/dbconfig.js');
var doconnect = function(cb) {
oracledb.getConnection(
{
user : dbConfig.user,
password : dbConfig.password,
connectString : dbConfig.connectString
},
cb);
console.log('Connection was successful!');
};
var dorelease = function(conn) {
conn.close(function (err) {
if (err)
console.error(err.message);
});
console.log('Connection closed successfully!');
};
// Optional Object Output Format
var doquery_object = function (conn, cb) {
conn.execute(
"SELECT d.dc_name,c.cobrand_name,c.cobrand_id,c.IS_CACHERUN_DISABLED,c.is_channel,c.environment,c.COBRAND_STATUS_ID,c.deployment_mode,c.db_name,c.gatherer_group FROM cobrand_master c,dc_master d where d.dc_id = c.dc_id ORDER BY c.display_priority",
{},
{ outFormat: oracledb.OBJECT },
function(err, result)
{
if (err) {
return cb(err, conn);
} else {
console.log("----- cobrand_master details (OBJECT output format) --------");
console.log(result.rows);
return cb(null, conn);
}
});
};
async.waterfall(
[
doconnect,
doquery_object
],
function (err, conn) {
if (err) { console.error("In waterfall error cb: ==>", err, "<=="); }
if (conn)
dorelease(conn);
});
When I am doing npm start in command prompt, I am getting the data in JSON format.I want to save the output and send it over angular js UI. Since I am new to this, can anyone please help me with simple steps to do it with an example.
Related
I have a 2 different endpoint in index.js file. The android app calls and retrieves data with these endpoints from local MongoDB database. The first endpoint "/AddArac" works fine in starting but if ı Call "/AddArac" after calling "/ListArac" it gives an error.
For example:
/AddArac - /ListArac => this order is ok for both.
/ListArac - /AddArac => this order is not ok for /AddArac.
var mongodb = require('mongodb');
var objectID = mongodb.ObjectID;
var express = require('express');
var bodyparser = require('body-parser');
var app = express();
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({ extended: true }));
var mongoClient = mongodb.MongoClient;
var url = 'mongodb://localhost:27017'
app.post('/AddArac', (request, response, next) => {
mongoClient.connect(url, { useNewUrlParser: true }, function (err, client) {
var db = client.db('basaranSMSDev');
var data = request.body;
var name = data.name;
var plaka = data.plaka;
var tel = data.tel;
var json = { 'name': name, 'plaka': plaka, 'tel': tel };
db.collection('Araclar').find({ 'plaka': plaka }).count(function (error, number) {
if (number > 0) {
response.json('Araç daha önce kaydedilmiş.');
}
else {
db.collection('Araclar').insertOne(json, function (error, res) {
if (err) {
res.json('Araç kaydında hata oluştu tekrar deneyiniz.');
console.log('Arac eklenemedi', err);
}
else {
res.json('Araç eklendi');
console.log('Arac eklendi.');
}
});
}
client.close();
});
});
});
app.get('/ListArac', (req, res) => {
mongoClient.connect(url, { useNewUrlParser: true }, function (err, client) {
var db = client.db('basaranSMSDev');
var items = db.collection('Araclar').find().toArray();
items.then(function (result) {
console.log(result)
res.json(result)
})
console.log(items);
client.close();
});
})
The error:
C:\Users\Dilber\Basarannodejs\node_modules\mongodb\lib\utils.js:106
throw err;
^
TypeError: res.json is not a function
at C:\Users\Dilber\Basarannodejs\index.js:138:29
at executeCallback (C:\Users\Dilber\Basarannodejs\node_modules\mongodb\lib\operations\execute_operation.js:70:5)
at C:\Users\Dilber\Basarannodejs\node_modules\mongodb\lib\operations\insert_one.js:34:21
at handleCallback (C:\Users\Dilber\Basarannodejs\node_modules\mongodb\lib\utils.js:102:55)
at C:\Users\Dilber\Basarannodejs\node_modules\mongodb\lib\operations\common_functions.js:269:5
at C:\Users\Dilber\Basarannodejs\node_modules\mongodb\lib\core\connection\pool.js:405:18
at processTicksAndRejections (node:internal/process/task_queues:75:11)
in your code
app.post('/AddArac', (request, response, next) => {
you have used response as the response parameter.
but you used res in
if (err) {
res.json('Araç kaydında hata oluştu tekrar deneyiniz.');
res is not a function in
db.collection('Araclar').insertOne(json, function (error, res) {
I have 2 endpoints in Node.js index.js file.
First one is working well and it turns 200 OK. But the second one is not working, it turns 404 Error. I could not find the reason.
First endpoint is /AddArac. It works well and adds data to MongoDB database
Second endpoint is /AddMesaj. It doesn't work. Both endpoints have the same logic.
My nodejs file is here:
`
var mongodb = require('mongodb');
var objectID = mongodb.ObjectID;
var express = require('express');
var bodyparser = require('body-parser');
var app = express();
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({ extended: true }));
var mongoClient = mongodb.MongoClient;
var url = 'mongodb://localhost:27017'
mongoClient.connect(url, { useNewUrlParser: true }, function (err, client) {
if (err) {
console.log('Connection Fail', err);
}
else {
var db = client.db('basaranSMSDev');
//Add arac
app.post('/AddArac', (request, response, next) => {
var data = request.body;
var name = data.name;
var plaka = data.plaka;
var tel = data.tel;
var json = { 'name': name, 'plaka': plaka, 'tel': tel };
db.collection('Araclar').find({ 'plaka': plaka }).count(function (error, number) {
if (number > 0) {
response.json('Araç daha önce kaydedilmiş.');
}
else {
db.collection('Araclar').insertOne(json, function (error, res) {
if (err) {
response.json('Araç kaydında hata oluştu tekrar deneyiniz.');
console.log('Arac eklenemedi', err);
}
else {
response.json('Araç eklendi');
console.log('Arac eklendi.');
}
});
}
});
});
//Araç listesi getirme
//add mesaj
app.post('AddMesaj', (request, response, next) => {
var data_mes = request.body;
var plaka = data_mes.plaka;
var mtext = data_mes.text;
var driver = data_mes.driver;
var date = data_mes.tarih;
var json = { 'plaka': plaka, 'text': mtext, 'driver': driver, 'date': date };
db.collection('Mesajlar').insertOne(json, function (error, res) {
if (err) {
response.json('Mesaj gönderiminde hata oluştu tekrar deneyiniz.');
console.log('Mesaj eklenemedi', err);
}
else {
response.json('Mesaj gönderildi');
console.log('Mesaj eklendi.');
}
});
});
app.listen(3000, () => {
console.log('Connection ok');
});
//Mesaj listesi getirme
}
}
);
`
Change to:
app.post('/AddMesaj', (request, response, next) => {
Moving the database connection code another nodejs file, no connection object is returned.
I can write data to MongoDB in nodejs. All db connection code is written in a single .js file.
Now I try to seperate the db connection code to another .js file, and now it seems that no connection can be made successfully.
Here is the working code in 1 .js file:
const mongoDBIP = '192.168.1.71';
const mongoDBPort = 27017;
const mongo= require('mongodb').MongoClient;
const mongoURL = 'mongodb://<mongo admin>:<password>#'+`${mongoDBIP}`+':'+`${mongoDBPort}`;
...
mongo.connect(mongoURL, { useNewUrlParser: true, useUnifiedTopology: true }, (err, db) => {
if (err) {}
var dbo = db.db(<databaseName>);
var collection = dbo.collection('messages');
collection.insertOne(message, (err, result) => {
if (err) {}
})
...
Now I break this into 2 .js files:
var _mongo = require('./mongodb.js');
var mongoDBO = _mongo.mongoDBO;
...
var dbo = mongoDBO('feathers');
console.log('DBO:' + dbo); <-- here, dbo is NULL
var collection = dbo.collection('messages');
collection.insertOne(message, (err, result) => {
if (err) {}
})
...
Here is the content of mongodb.js:
// mongodb.js
const mongoDBIP = '192.168.1.71';
const mongoDBPort = 27017;
const mongoClient = require('mongodb').MongoClient;
const mongoURL = 'mongodb://<mongo admin>:<password>#'+`${mongoDBIP}`+':'+`${mongoDBPort}`;
function mongoDBO(database) {
var dbo;
mongoClient.connect(mongoURL, { useNewUrlParser: true, useUnifiedTopology: true }, (err, dbase) => {
if (err) {}
dbo = dbase.db(database); <-- here, dbo is NULL
});
return dbo;
}
module.exports = Object.freeze({
mongoDBO
});
I expect the object "dbo" in mongodb.js would not be NULL, but it is. This occurs under both nodejs v10.x and v12.x.
Here is an implementation what Tom explained, in case you need.
mongodb.js
const mongoClient = require('mongodb').MongoClient;
const mongoDBIP = '192.168.1.71';
const mongoDBPort = 27017;
const mongoURL = 'mongodb://<mongo admin>:<password>#'+`${mongoDBIP}`+':'+`${mongoDBPort}`;
let _db;
const initDb = callback => {
if (_db) {
console.log('Db is already initialized!');
return callback(null, _db);
}
mongoClient .connect(mongoURL)
.then(client => {
_db = client;
callback(null, _db);
})
.catch(err => {
callback(err);
});
};
const getDb = () => {
if (!_db) {
throw Error('Db not initialized');
}
return _db;
};
module.exports = {
initDb,
getDb
};
Initialize it in your main file (index, app or server.js)
const mongodb = require('./mongodb');
mongodb.initDb((err, mongodb ) => {
if (err) {
console.log(err);
} else {
app.listen(3000);
}
});
And use it in your routes:
const mongodb = require('./mongodb');
mongodb.getDb()
.db()
.collection('your_collection_name')....
You are trying to return a value that does not exist yet. dbo is not set until the callback function is invoked. The original function has already returned at that point.
You must alter your mongodb.js file to export a function that takes a callback, and passes dbo to that callback when it is ready.
I recommend reading up on callbacks and asynchronous programming.
Try like this
const mongoDBIP = "192.168.1.71";
const mongoDBPort = 27017;
const mongoClient = require("mongodb").MongoClient;
const mongoURL =
"mongodb://<mongo admin>:<password>#" +
`${mongoDBIP}` +
":" +
`${mongoDBPort}`;
var mongoDBO = database =>
new Promise((resolve, reject) => {
mongoClient.connect(
mongoURL,
{ useNewUrlParser: true, useUnifiedTopology: true },
(err, dbase) => {
if (err) {
reject(err);
}
resolve(dbase);
}
);
});
module.exports = Object.freeze({
mongoDBO
});
&
var _mongo = require('./mongodb.js');
var mongoDBO = _mongo.mongoDBO;
...
try {
let dbo = mongoDBO('feathers');
console.log('DBO:' + dbo); <-- here, dbo is NULL
var collection = dbo.collection('messages');
collection.insertOne(message, (err, result) => {
if (err) {}
})
} catch (error) {
console.log(connection failed)
}
...
I have searched for the solution of the error specified in title.
MongoError: server instance pool was destroyed
I believe it is because misplacement of db.close(). But I am nesting dbo.collection and unable to get the exact solution of this error.
Firstly, I am fetching data (array of ids having status 0) from database and then I am concatenating (each app-id) them one by one with URL to get desired appUrl which will be used for crawling data one by one and then crawled data is meant to be stored into another collection of mongoDB. This process will repeat for each id in the array. But my code is having error of "server instance pool gets destroyed" before storing data into collection. I am doing misplacement of db.close() but I am unable to resolve this. Please help me resolving this error
Here is my code
///* global sitehead */
const request = require('request');
const cheerio = require('cheerio');
//const response = require('response');
const fs = require('fs');
const express = require('express');
const app = express();
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
var dateTime = require('node-datetime');
MongoClient.connect(url, {useNewUrlParser: true}, function (err, db) {
if (err) {
throw err;
} else {
var dbo = db.db("WebCrawler");
var app_id;
var appUrl;
let arr = [];
dbo.collection("Unique_Apps").find({"Post_Status": 0}, {projection: {_id: 0, App_Id: 1}}).toArray(function (err, result)
{
// console.log(result);
if (err) {
throw err;
// console.log(err);
} else {
for (var i = 0; i < result.length; i++)
{
arr[i] = result[i];
}
arr.forEach((el) => {
app_id = el.App_Id;
//console.log(app_id);
appUrl = 'https://play.google.com/store/apps/details?id=' + app_id;
console.log(appUrl);
request(appUrl, function (error, response, html) {
if (!error && response.statusCode === 200) {
//START Crawling ###########
const $ = cheerio.load(html); //cheerio
const appTitle = $('.AHFaub');
const iconUrl = $('.T75of.sHb2Xb').attr("src");
const developedBy = $('.T32cc.UAO9ie').children().eq(0);
const category = $('.T32cc.UAO9ie').children().eq(1);
//store in database collection: "Single_App_Data_Post"
var curr_obj = {App_Id: app_id, App_Name: appTitle.text(),
Icon_Url: iconUrl, Price: "Free", Developed_By: developedBy.text(),
Category: category.text()
};
dbo.collection("Single_App_Data_Post").insertOne(curr_obj, function (err, res) {
console.log("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
if (err) {
throw err;
// console.log(err);
} else {
console.log("inserted....");
} //main else
});
dbo.collection("Unique_Apps").updateOne({App_Id: app_id}, {$set: {Post_Status: 0}}, function (err, res) {
if (err)
throw err;
console.log("1 document updated");
//dbo.close();
});
} else
{
throw error;
}
});
});
}
db.close();
});
} //else
}); //mongoClient connect db
Output
The following is a good start about how to turn callback into promises. Try to use it, execute the code block, by block, understand it and then add your updateOne/insertOne requests into it.
const request = require('request');
const cheerio = require('cheerio');
const fs = require('fs');
const express = require('express');
const app = express();
const MongoClient = require('mongodb').MongoClient;
const dateTime = require('node-datetime');
// Class used to handle the database basic interractions
class DB {
constructor() {
this.db = false;
this.url = "mongodb://localhost:27017/";
}
// Do connect to the database
connect() {
return new Promise((resolve, reject) => {
MongoClient.connect(this.url, {
useNewUrlParser: true,
}, (err, db) => {
if (err) {
console.log('error mongodb connect');
return reject(err);
}
this.db = db;
return resolve(db);
});
});
}
disconnect() {
db.close();
this.db = false;
}
getCollection(name) {
return this.db.db(name);
}
}
// Get the data from the database
function getAppsIds(dbObj) {
return new Promise((resolve, reject) => {
const dbo = dbObj.getCollection('WebCrawler');
dbo.collection('Unique_Apps').find({
'Post_Status': 0,
}, {
projection: {
_id: 0,
App_Id: 1,
}
}).toArray(function(err, result) {
if (err) {
return reject(err);
}
return resolve(result);
});
});
}
function requestPlayStore(idApp) {
return new Promise((resolve, reject) => {
const appUrl = `https://play.google.com/store/apps/details?id=${app_id}`;
request(appUrl, function(error, response, html) {
if (error || response.statusCode !== 200) {
return reject(error);
}
return resolve({
response,
html,
});
});
});
}
// Do treat one id app at a time
function treatOneIdApp(dbObj, idApp) {
return requestPlayStore(idApp)
.then(({
response,
html,
}) => {
// Perform your requests here updateOne and insertOne ...
});
}
const dbObj = new DB();
dbObj.connect()
.then(() => getAppsIds(dbObj))
.then(rets => Promise.all(rets.map(x => treatOneIdApp(dbObj, x.App_Id))))
.then(() => dbObj.disconnect())
.catch((err) => {
console.log(err);
});
I want to save and retrieve a Word doc and PDF file with a size of 1 MB, directly in MongoDB with Node.js. How can I do this is there any article explain about it or can some one help me on this.
Here is the standalone node js code to save the file as binary data in MongoDB. As the maximum file size is 1MB, you can save it in normal collection rather than GridFs.
This can be extended to run as web apps using "express" or "hapi" frameworks. You may need to refer the respective tutorial for that.
Save the file as binary data in MongoDB:-
Note: I have the sample file in "docs" directory. So, I have prefixed it with docs (i.e. "/docs/testplan.docx"). You can remove that if you don't need it.
var MongoClient = require('mongodb').MongoClient;
var Binary = MongoClient.Binary;
var fs = require('fs');
var assert = require('assert');
var url = 'mongodb://localhost:27017/test';
var binaryFileData = fs.readFileSync(__dirname + "/docs/testplan.docx");
var insertDocument = function(db, callback) {
db.collection('file_save').insertOne( {
"fileName" : "testplan.docx",
"fileData" : binaryFileData
}, function(err, result) {
assert.equal(err, null);
console.log("Inserted a document into the collection.");
callback();
});
};
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
insertDocument(db, function() {
console.log("Closing the database connection...")
db.close();
});
});
Read the file data and save it to disk:-
var MongoClient = require('mongodb').MongoClient;
var Binary = MongoClient.Binary;
var fs = require('fs');
var assert = require('assert');
var url = 'mongodb://localhost:27017/test';
var findDocument = function (fileName, db, callback) {
db.collection('file_save').findOne({"fileName" : fileName }, function (err, document) {
assert.equal(null, err);
console.log(document.fileName);
fs.writeFileSync("testplan_out.docx", document.fileData.buffer);
console.log("File has been written to disk");
callback();
});
};
MongoClient.connect(url, function (err, db) {
assert.equal(null, err);
findDocument("testplan.docx", db, function () {
db.close();
});
});
that works perfectly alright! But I'm trying to upload the document from the POSTMAN and I'm developing my project with MEAN stack.
//document-model.js
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var documentSchema = {
docFile: {
type: String
}
};
mongoose.model('FileDocument', documentSchema);
//document-route.js
var express = require('express'),
documentRoute = express.Router(),
document = require('../controllers/document-controller');
documentRoute.post('/upload', document.uploadDocument);
module.exports = documentRoute;
//document-controller.js
var document = {
uploadDocument: function (req, res) {
var fileDocument = new FileDocument({
docFile: req.body.docFile
});
fileDocument.save(function (err, result) {
if (err) {
res.status(500).send(err);
} else {
res.status(200).send('Document Uploaded Successfully');
}
});
}
};
I was trying in this way but it is not uploading to mongodb the result is gives in the mongo shell.
{ "_id" : ObjectId("59693872b8b83f42b42a3b9f"), "docFile" : "", "__v" : 0 }