2017-03-01 5 views
0

Ich bin ziemlich neu mit NodeJS und es verursacht offensichtlich einige Probleme, da die Dinge sehr asynchron sind.Wie synchron Instanz und erstellen in LoopBack

Ich versuche zu Instanzen zu finden, die verwendet werden müssen, um eine neue (Relation) zu erstellen: countryId und clientId.

Offensichtlich geschieht Dinge asynchron, so dass die Variablen nicht definiert sind. Wie mache ich es synchron?

Das ist mein Boot-Skript in LoopBack

module.exports = function(app) { 
    var Client = app.models.Client 
    var Country = app.models.Country 
    var BillingAddress = app.models.BillingAddress 

    Client.create([ 
     {username: '[email protected]', email: '[email protected]', password: 'nasapassword'}, 
     {username: '[email protected]', email: '[email protected]', password: 'nanapassword'} 
    ], function(err, clients){ 
     if(err) throw err 
    }) 


    Country.findOne({where: {name: 'Spain'}}, 
     function(err, country) { 
      if(err) throw err 
     } 
    ) 

    Client.findOne({where: {email: '[email protected]'}}, 
     function(err, client) { 
      if(err) throw err 
     } 
    ) 

    BillingAddress.create([ 
     {first_name: 'Jimbo', last_name: 'Juice', address_line_1: 'Loool street', postal_code: '23232', countryId: country.id, clientId: client.id} 
    ], function(err, billingaddress) { 
     if(err) throw err 

    }) 
} 

Antwort

1

Es ist nicht möglich. Die Async-Funktion sollte als Async-Aufruf behandelt werden.

Sie können async Modul verwenden.

module.exports = function(app, cb) { 
    var Client = app.models.Client; 
    var Country = app.models.Country; 
    var BillingAddress = app.models.BillingAddress; 

var clientData = [{username: '[email protected]', email: '[email protected]', password: 'nasapassword'}, 
     {username: '[email protected]', email: '[email protected]', password: 'nanapassword'}]; 

async.parallel({ 
    clients: function(callback){ 
    async.map(clientData, Client.create.bind(Client), function(err, clients){ 
     if(err) return callback(err); 
     callback(null, clients); 
    });  
    }, 
    country: function(callback){ 
    Country.findOne({where: {name: 'Spain'}}, callback); 
    }, 
    nasaClient: function(callback){ 
    Client.findOne({where: {email: '[email protected]'}}, callback); 
    } 
}, function(err, results){ 
    if(err) return cb(err); 

    BillingAddress.create([ 
     {first_name: 'Jimbo', last_name: 'Juice', address_line_1: 'Loool street', postal_code: '23232', countryId: results.country.id, clientId: results.nasaClient.id} 
    ], function(err, billingaddress) { 
     if(err) return cb(err); 
     cb(null); 
    }); 
}); 

} 

Einige Punkte:

  • Das Boot-Skript sollte async sein
  • Vermeiden Ausnahmen werfen. von callback s
+0

umgehen Just So enthalten i "async" und ich bekomme folgende Fehlermeldung, wenn ich den Code ausführen 'Typeerror: this.getDataSource ist kein function' – JavaCake

+0

@JavaCake Wo sehen Sie' this.getDataSource verwenden ' ? –

+0

Nirgendwo, es ist das erste Mal, dass ich das sehe. – JavaCake

Verwandte Themen