Skip to content
This repository has been archived by the owner on Mar 4, 2019. It is now read-only.

Script is blocked when i have sync queries on it #190

Closed
jfbaquerocelis opened this issue Jan 29, 2016 · 3 comments
Closed

Script is blocked when i have sync queries on it #190

jfbaquerocelis opened this issue Jan 29, 2016 · 3 comments

Comments

@jfbaquerocelis
Copy link

Hi everyone!
Few days ago, I created a script for consult an information. But when i try excecute the script, this is blocked. It´s my code:

// The connection to database is Sync method
const massiveInstance = require('./app/models/connection'),
    moment = require('moment'),
    database = massiveInstance(),
    _ = require('underscore')

const scanDocuments = () => {
    moment().format()
    let today = moment().format('YYYY-MM-DD')
    new Promise((resolve, reject) => {
        database.getall_vehicle_person((err, result) => {
            if (err){
                console.error(err)
            }else{
               // This methods are not important, only validate info
                let person_vehicle = format_object(result),
                     notifications = validate_documents_vehicle(today, person_vehicle),
                     licenses = validate_license_person(today, person_vehicle),
                     final_notifications = _.union(licenses, notifications)
            }
        })
        resolve(final_notifications, today)
    })
    .then((final_notifications, today) => {
            _.each(final_notifications, (value, key) => {
                let notify = database.notification.findOneSync({id : value.id})
                    if (_.isEmpty(notify)){
                        database.notification.insertSync(value)
                    }else{
                        if (notify.signal === 'warning' && value.signal === 'danger'){
                            database.notification.updateSync(value)
                        }
                    }
            })
            process.exit(0)
    })
}

Note: The process.exit(0) is for kill the process, because when i not put this code, my script never stop. Please explain me this.

Which is the problem?
Because, when i use querys async the process is killed whitout consult the database.

Thanks for read! ☺️ 🙏

@xivSolutions
Copy link
Collaborator

Why are you using the database synchronously inside an async architecture?

@jfbaquerocelis
Copy link
Author

Because, I have several modules in my app and i need to save the database instance in a variable. Now, How could i do? Should i insert the code in the callback ?

      massive.connect({ connString : connString },
                 (err, db) => {
                         // My Code...
                 })

😊

@xivSolutions
Copy link
Collaborator

var database;

massive.connect({connectionString: "connString"}, function(err,db){
  database = db;

    // Your code....
});

Take a lookk at the tests and the readme for some more usage examples.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants