因此,我要向数据库中添加一个场所,然后向数据库中添加一个特殊项。一个场所可以有1到许多特惠,所以我试图在第三个.then()
下创建一个关系。问题是,在建立并将特殊项添加到数据库之前,正在执行第三个.then()
。我需要先将建立和特殊添加到数据库,因为我需要获取它们的objectIds var establishmentObjectId;
和var specialObjectId;
我该如何确保在建立关系之前检索到场所和特殊的objectId?
//Save the establishment objectId
var establishmentObjectId;
//Save the special objectID
var specialObjectId;
//Save establishment
.then(() => {
//Get the add form data
var addName = document.getElementById('add_name');
var addCountry = document.getElementById('add_country');
var addEstablishmentType = document.getElementById('add_establishment_type');
var addCuisineType = document.getElementById('add_cusine_type');
//Create establishment from user's enteries
var establishment = {
Name: addName.value,
Address: addAddress.value,
Suburb: addSuburb.value,
Country: addCountry.value,
Cuisine_Type: addCuisineType.value,
Establishment_Type: addEstablishmentType.value
}
//Save establishment to db
Backendless.Data.of('Establishment').save( establishment )
.then( function( savedObject ) {
establishmentObjectId = savedObject.objectId;
console.log( "new Establishment instance has been saved" );
})
.catch( function( error ) {
console.log( "an error has occurred " + error.message );
});
})
//Save special
.then(() => {
//Get the add form data
var addCategory = document.getElementById('add_category');
var addTypeOfSpecial = document.getElementById('add_type_of_special');
var addDescription = document.getElementById('add_description');
//Create special from user's enteries
var special = {
Category: addCategory.value,
Type_Of_Special: addTypeOfSpecial.value,
Description: addDescription.value
}
//Save special to db
Backendless.Data.of('Special').save( special )
.then( function( savedObject ) {
specialObjectId = savedObject.objectId;
console.log( "new Special instance has been saved" );
})
.catch( function( error ) {
console.log( "an error has occurred " + error.message );
});
})
//Add special to establishment/form relation
.then(() => {
//These are undefined even though they are declared above
console.log(establishmentObjectId);
console.log(specialObjectId);
var parentObject = { objectId:establishmentObjectId };
var childObject = { objectId:specialObjectId };
var children = [ childObject ];
Backendless.Data.of( "Establishment" ).addRelation( parentObject, "establishmentSpecials", children )
.then( function( count ) {
console.log( "relation has been set" );
})
.catch( function( error ) {
console.log( "server reported an error - " + error.message );
});
})
非常感谢
答案 0 :(得分:2)
无论何时,只要有99%的时间处于Promise链中,当您创建新的Promise(使用API调用或使用new Promise
等)时,都应将其返回或以其他方式放入以及其他内容(例如Promise.all
并返回 that )。这将意味着可以在更高层次上处理可能的拒绝,并且意味着链中的 next .then
仅在先前的.then
完成后才开始。 >
每更改一次
Backendless.Data.of(
到
return Backendless.Data.of(
此外,您可能不想在每个级别上都catch
,就像您目前正在做的那样-每当您catch
时,您都会拒绝接受承诺到已解决的Promise中,随后的.then
可能会运行在先前.then
中的所有内容均已成功完成的假设下。对于此类顺序异步操作,如果任何地方错误,您可能希望停止整个过程,而不是尝试继续进行(可能会导致错误)。
例如,如果Backendless.Data.of('Establishment').save
失败,则establishmentObjectId
永远不会分配给它,而在最后的.then
中尝试稍后使用它将无效。与specialObjectId
一样。
对于此代码,还有一个更好的模式可以遵循:不要使用两个单独的.then
,一个要获取establishmentObjectId
,另一个要获取specialObjectId
,请考虑让它们操作并行运行,并在通过.then
完成后运行最后的Promise.all
。类似于:
const getEstablishmentObjectId = () => {
//Get the add form data
// ...
//Create establishment from user's enteries
var establishment = {
// ...
}
//Save establishment to db
return Backendless.Data.of('Establishment').save(establishment)
.then(savedObject => savedObject.objectId);
};
const getSpecialObjectId = () => {
// ...
return Backendless.Data.of('Special').save(special)
.then(savedObject => savedObject.objectId);
};
Promise.all([
getEstablishmentObjectId(),
getSpecialObjectId(),
]).then(([establishmentObjectId, specialObjectId]) => {
console.log(establishmentObjectId);
console.log(specialObjectId);
var parentObject = {
objectId: establishmentObjectId
};
// etc
})
.catch((error) => {
console.log('Error', error);
});