0
我对NodeJS非常新,它显然会导致一些问题,因为事情具有非同步性质。如何同步查找实例并在LoopBack中创建
我想找到必须用来创建一个新的(关系)的实例:countryId
和clientId
。
显然事情是异步发生的,所以变量是未定义的。我如何使它同步?
这是回送
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
})
}
处理它,所以我列入“异步”和我得到以下错误,当我运行的代码'类型错误:this.getDataSource不是function' – JavaCake
@JavaCake在哪里使用''this.getDataSource ? –
不通,它是我第一次看到这个.. – JavaCake