I'm using nano db library (also try this with cloudant ant library)
for node js
I have this sample code :
exports.insert = function (db_name, data, callback) {
var db_name=db_name; if(!db){ db = nano.use(db_name); } console.log(`try to make bulk copy to Couchdb`); db.bulk(data, function (err, body) {
if (!err) {
console.log(`data in db ${db_name} inserted successfully`);
return callback(null, body);
}
console.log(`err ouccre ${err}`);
return callback(err);
}); }
The "data" variable is an object that contain the 'docs' property and
'docs' contain array.
I'ts always do the bulk work and put the docs array into my cloudnat
db , but
Often doesn't return any callback (err / success) to my node js
process
and it stuck...
Any suggestion?
Related
Accordingly with nodejs sqlite library (https://github.com/mapbox/node-sqlite3) you can serialize a sql transaction with db.serialize function. All executions in this function will be executed in the specified order.
But with a for loop how can i get the information about the transaction will be success or fail?
function transaction(callback) {
db.serialize(function() {
db.run("BEGIN");
for(...) {
db.run("INSERT", function(err) {
//this code is not serialized with the parent serialize function (this is my problem)
//if one run throw error i must resolve the callback with the error code
if (err)
callback("error");
});
}
db.run("END");
callback("ok");
}
}
Before start transaction you can define some vars e.g. var results = [];. On complete each query inside callback you can collect error or null as results.push(err) and check results.length. When results.length equals to length of for then all queries executed and now check results.every((i) => i === null) value. If true then do commit else rollback.
Much better to control flow is use async module.
'use strict'
var async = require('async');
...
db.run('begin', function(err){
if (err)
return console.log(err);
let run = (sql, callback) => db.run(sql, callback);
async.eachSeries(sqls, run, function(err){
db.run((err) ? 'rollback' : 'commit', ...)
})
})
If you add begin to sqls then code will be more simple.
I am using mongodb driver for nodejs.
I am getting below error while updating a record.
{"name":"MongoError","message":"selector must be a valid JavaScript
object","driver":true}
Here is my script :
MongoClient.connect(url, function (err, db) {
if (err)
{
console.log('Unable to connect to the mongoDB server. Error:', err);
return;
}
var collName = "bank";
var SelectParas = {"name":"ABC"};
var UpdateValues = {"name":"PQR"};
db.collection(collName).update(collName,SelectParas,{$set:UpdateValues},function (err,numUpdated){
if(err)
{
console.log('err');
console.log(err);
return;
}
if(numUpdated)
{
console.log('Updated Successfully %d document(s).', numUpdated);
}
db.close();
});
});
I can write the below line in mongo console & it works.
db.bank.update({"name":"ABC"},{$set:{"name":"PQR"}})
You are passing collecion name i.e. a string as find query of the update. Need not pass collecton name there.
db.collection(collName).update(collName,SelectParas,{$set:UpdateValues},function (err,numUpdated)
// collName need not pass in the update function.
Need to use
db.collection(collName).update(SelectParas,{$set:UpdateValues},function (err,numUpdated) instead.
I am not receiving this error all the time but for specifics arrays. I am trying to insert a JSON object into mongodb collection using node.js mongodb native driver. This JSON object has couple of string attributes and a big string array attribute. Array could have thousands of string items.
My JSON looks like this
{
FileName :"504-2345.txt",
SIMs :["8931440400012","893144040001","4000130360507",.........]
}
Any idea when MongoDB throws RangeError: attempt to write outside buffer bounds? Please suggest
Below method insert the data in Mongodb
Insert: function (data, type, callback) {
MongoClient.connect(url, function (err, db) {
// logger.log("info","Before Inserting documents into "+type +" documents =>"+data.length);
if (err) {
logger.log("error", err);
}
var collection = db.collection(type);
// Insert some documents
collection.insertOne(data, function (err, result) {
if (err) {
logger.log("error", " Error for Data while inserting Error =" + err);
}
else {
db.close();
if (result.ops) {
callback(err, result.ops[0]);
}
}
});
});
},
The document should be less than 16mb. Otherwise it get exceptions.You can refer this link for more details. You can use gridFs in here.
The 'GetUsers' function is running and it returns the correct values as expected. However, res.send(users) is running before the nested function has completed with the value so it is undefined. I have tried doing the res.send(users) from inside the nested function but it doesn't recognize the function. I have also tried using return users but can't get it to work. Any help would be appreciated!
app.get('/GetUsers', function (req, res){
var url = 'mongodb://localhost:27017/users';
var users = "";
MongoClient.connect(url, function (err, db) {
if (err) {
console.log('Unable to connect to the mongoDB server. Error:', err);
} else {
console.log('Connection established to', url);
// Get the documents collection
var collection = db.collection('users');
// Get some users
collection.find().toArray(function (err, res) {
if (err) {
console.log(err);
} else {
console.log('Got Information from db.');
}
//Close connection
db.close();
console.log('db closed.');
users = res;
console.log(users);
});
}
});
res.send(users);
});
Just move res.send(users) inside your callback before/after you call console.log(users).
You reused the variable res in the toArray call, so please rename it there: ...toArray(function (err, res)... to ...toArray(function (err, result)... and also in users = res; to users = result;.
this is async call it will send the response and will not wait for the database connection function.
write res.send in call back function
write your res.send function where you are closing your data base connection.
here is detail How do I return the response from an asynchronous call?
Found the answer!!!
I needed to use res.send(users) in my callback after I had closed the database as pointed out by the users suggestions above. HOWEVER, that alone wasn't working as res.send(users) was erroring as it wasn't recognized as a function. I had to use res.send({'matched':users}) and then access my response objects on the client side like this: response.matched.objects etc.
Thanks for your help everyone!
I used following method to create a node in Neo4j using nodejs. I want to create a node with its label as well as some property.
var query = [
"CREATE (n:TYPE {props})",
"RETURN n",
].join('\n').replace('TYPE','PLAYER');
var params = {
props: data,
};
neo4jClient.query(query, params, function (err, results) {
if(err) return callback(err);
var node = neo4jClient.createNode(results[0].n._data.data);
var player = new Player(node);
node.save(function (err) {
console.dir(err);
if (err) return callback(err);
node.index(INDEX_NAME, INDEX_KEY, INDEX_VAL, function (err) {
if (err) return callback(err);
callback(null, player);
});
});
});
Problem :- Everything goes right but this method creates two nodes in neo4j. I don't know what's happening internally. Please give me some input in this problem.
You actually execute the query which creates the node (and would be good enough).
But then you go again and create a new node node.save() + node.index() both of which are not necessary as the CREATE query already did the work.
It works with following function.
neo4jClient.query(query, params, function (err, results) {
if(err) return callback(err);
var node = results[0].n;
var player = new Player(node);
node.index(INDEX_NAME, INDEX_KEY, INDEX_VAL, function (err) {
if (err) return callback(err);
console.log("=============================");
callback(null, player);
});
});
neo4jClient.query itself create a node so if you call function createNode again then it also create second node. So if you remove createNode function then .query function create a node and also persistent in database as well as in nodejs.