I'm using Node with postgresql. I'm trying to do a knex query to the bd.
Why this query doesn't work without the then() call?
knex('test').insert(User1).then();
it's because the framework expects you to either fulfill the promise or to invoke any other provided output interfaces
to chain the promise output is one of the cleaner ways to end the query building step, see this little example, hope it helps.
I think it's expected behavior.
Related
I receive 3 post calls from client, let say in a second, and with nodejs-mongodb immediately(without any pause, sleep, etc) I try to insert the data that is posted in database using updateOne. All data is new, so in every call, insert would happen.
Here is the code (js):
const myCollection = mydb.collection("mydata")
myCollection.updateOne({name:req.data.name},{$set:{name:req.data.name, data:req.data.data}}, {upsert:true}, function(err, result) {console.log("UPDATEONE err: "+err)})
When I call just 1 time this updateOne, it works; 2 times successively, it works. But if I call 2+ times in succession, only the first two ones correctly inserted into database, and the rest, no.
The error that I get after updateOne is, MongoWriteConcernError: No write concern mode named 'majority;' found in replica set configuration. However, I always get this error, also even when the insertion is done correctly. So I don't think this is related to my problem.
Probably you will suggest to me to use updateMany, bulkWrite, etc. and you will be right, but I want to know the reason why after 2+ the insertion is not done.
Have in mind .updateOne() returns a Promise so it should be handled properly in order to avoid concurrency issues. More info about it here.
The error MongoWriteConcernError might be related to the connection string you are using. Check if there is any &w=majority and remove it as recommended here.
The database is in Azure cloud and not being used in production currently. There are 80.000 rows and a uprn is a VARCHAR(100);
I'm already using JOI to validate each UPRN as well;
I'm using KNEX with a SQL Server database with the following whereIn query:
knex(LOCATIONS.table).whereIn(LOCATIONS.uprn, req.body.uprns)
but this takes 8-12s to complete and sometimes timesout. if I use .toQuery() on the same thing, SSMS will return the result within 1-2.
If I do a raw query, the resulting .toQuery() or toString() works in SSMS and returns results. But if I try to use the raw directly, it will return 0 results.
I'm looking to either fix what's making whereIn so slow or get the raw query working.
EDIT 1:
After much debugging and trying -- it seems that the bug is due to how knex deals with arrays, so I made a for-of loop to add ? ? ? for each array element and then inputed the array for all params.
This led me to realizing the performance issue is due to SQL server way of parameterising.
I ended up building a raw query string with all of the parameters and validating the input with Joi string/regex config:
Joi.string()
.min(1)
.max(35)
.regex(/^[a-z\d\-_\s]+$/i)
allowing only for alphanumeric, dashes and spaces which should prevent sql injection.
I'm going to look deeper into security issues with this and might make a separate login that can only SELECT data from that table and nothing more to run with these queries.
Needed to just handle it raw and validate separately.
I'm working on a node.js app that uses MongoDB and I read this from the docs:
db.collection
Fetch a specific collection (containing the actual collection information). If the application does not use strict mode you can can use it without a callback in the following way.
var collection = db.collection('mycollection');
First of all, what 'strict mode' is the doc referring to?
Also, is it a bad practice to grab the collection in this fashion? Without the callback, wouldn't I lose the ability to capture a potential connection error when trying to select the right collection?
db.collection('some_collection', function(err, collection) {
// query goes here
});
http://mongodb.github.io/node-mongodb-native/api-generated/db.html#collection
strict, (Boolean, default:false) returns an error if the collection
does not exist
Right there in the documentation.
That is there so your application may not create new collections itself and can only reference what has been created before. Hence the need for the callback, in order to trap the error.
It might be referring to Javascript's strict mode instead of a Mongo specific feature. strict mode enables some optional but backwards incompatible changes in the Javascript language that help catch some bugs:
What does "use strict" do in JavaScript, and what is the reasoning behind it?
I'm just learning how use mongoDb and I get confused by the documentation about the Collection.remove() method.
In first place, I tried to follow this doc:
http://docs.mongodb.org/manual/reference/method/db.collection.remove/
But I got the message Error: Cannot use a writeConcern without a provided callback, so I searchd why and I found this documentation then: http://mongodb.github.io/node-mongodb-native/api-generated/collection.html
In the first one there are two args, in the second there are three.
Then I did a console.log((mongodb.Collection(db, 'user').remove).toString());
And I got function remove(selector, options, callback)...
So now I just don't understand what's going on here, Is there different kind of Collection class? I whish to understand which documentation I should follow.
It seems like you just have to provide a callback-function:
Collection.remove(function(err, removedCount) {
//your next actions
});
From documentation:
[callback] (function) – must be provided if you performing a remove with a writeconcern
I am facing a problem with mongoose Model.create method.
When i call
Model.create(arrayOfThousandDocs, function(err){});
After 15 min (sufficient for all the docs to get saved) when i switch to mongo shell and query upon total no of docs saved
then i find only something around 700-800 (no of docs saved varies every time Model.create is called).
And mongoose or mongo returns no any error.
Have anyone faced the same bug?
Please tell me how to resolve it.
it may be because, Model.create uses forEach method, and that is not suitable in nodejs asynchronous mode programming.. Please correct me if if i am wrong..
and suggest your views..
here is the source : http://mongoosejs.com/docs/api.html#model_Model.create