Script Node Lock

I use Node.js to write system scripts that run on the server. Due to the asynchronous nature of Node, my script exits before the database calls have a chance to complete and nothing is ever written to the database.

I use Mongoose as ORM and talk to MongoDB if that matters. Node.js offers calls to the SYNCHRONOUS method precisely for this reason, for example: https://nodejs.org/api/child_process.html

I think my questions are:

1) Does the mongoose provide a way to block, so my script might wait for the database call to return?

2) If not, is there another method that I should consider, and not something like:

(function wait () {
   if (!SOME_EXIT_CONDITION) setTimeout(wait, 1000);
})();

3) Is Node not the best scripting tool? I love Node for developing web applications and can write nested callbacks or work with promises all day. But what about the scripting language?

EDIT ---------------------------------------------- -

The following is a brief example script for clarity:

#!/usr/bin/env node
# Please note the above that this is a bash script

var schema = mongoose.Schema({
   // ... attributes ...
});
var model = new (mongoose.model('ModelObject'))();

model['attribute'] = 42;

console.log('This gets printed first');
model.save(function(err) {
  console.log('Nothing in the callback gets printed because callback is never called');
  if(err) { // Can't check for errors because this is never reached
    console.log('This never gets printed to the screen');
    console.log('And consequently nothing is ever saved to mongo');
  } else {
    console.log('This never gets printed either');
  }
});
console.log('This gets printed second');
+4
source share
3 answers

If your model is not saved, there is a Mongo error. Following the MongoDB conventions, you should check for errors:

model.save(function(error, savedItem) {
  if(error) {
    // nothing is saved
  }
});

Otherwise, have you considered using Promises? This is useful for chaining events and simplifying error handling.

Promise = require('bluebird');
Promise.promisifyAll(mongoose.Query.base);

model.saveAsync().then(function(savedItem) {
  // saved
})
.catch(function(error) {
  // handle error
});
+1
source

, , , .

var mongoose = require('mongoose'),
    model1 = mongoose.model('model1'),
    model2 = mongoose.model('model2');

model1.findOne({"type" : 'Active'}, function err(err, catConfig) {
    if(!err.error){
        //This will execute once above DB call is done!
        model2.findOne(condition).remove(function(err, gAnalysis) {
                //Lines of code that you want to execute after second DB call 
        });
    }
});
0

I don’t see you open the database connection, so preservingly saving the model instance does nothing, it doesn’t even cause an error callback ...

I checked the example below:

test.js

var mongoose = require('mongoose');

var kittySchema = mongoose.Schema({
  name: String
});

var Kitten = mongoose.model('Kitten', kittySchema);

mongoose.connect('mongodb://localhost:27017/test', function (err) {
  if (err) throw err;

  var silence = new Kitten({ name: 'Silence' });
  silence.save(function (err, saved) {
    if (err) throw err;

    console.log('Kitty Silence is saved!');

    mongoose.disconnect(function (err) {
      if (err) throw err;

      console.log('done...');
    });
  });
});

Launch node test.jsdisplays this on the console:

Kitty Silence is saved!
done...

and checking my local test database shows that the silence is really saved.

0
source

Source: https://habr.com/ru/post/1606363/


All Articles