我正在努力深入拯救工作,如下所述: http://www.baqend.com/guide/topics/deep-loading/#deep-saving
我想创建一个用户,然后立即为将来的数据生成一些占位符对象,然后将这些新创建的引用保存回用户表。
这是我的React Native代码:
handleRegister = (event) => {
this.setState({ loading: true })
username = this.state.cellphone.replace(/\D/g,'');
db.ready().then((db) => {
var newUser = new db.User({
'username': username
});
db.User.register(newUser, this.state.password)
.then(() => {
var newUserData = new db.UserData({
User: db.User.me,
});
var newUserDataPrivate = new db.UserDataPrivate({
User: db.User.me,
phone: username,
});
var newUserStatus = new db.UserStatus({
User: db.User.me,
status: 0,
licenseVerified: false,
backgroundChecked: false,
hourlyRate: 0,
lastUpdater: null,
});
newUserData.doNext = newUserDataPrivate;
newUserDataPrivate.doNext = newUserStatus;
newUserData.save({depth: 2})
.then (() => {
console.log(newUserData.id)
console.log(newUserDataPrivate.id)
console.log(newUserStatus.id)
//update the User with references after I get the IDs
//this.props.navigation.dispatch(createAccountResetStack)
})
})
.catch(error => {
alert(error.message)
})
})
}
运行此操作时,会发生以下情况:
1) User is created
2) Object in UserData is created with reference to newly created user id
3) the console.log for UserData returns the id of the UserData object
4) the console.log for UserDataPrivate prints null,
5) the console.log for UserStatus prints null
6) the UserDataPrivate and UserStatus objects are not saved to the database
关于我在这里做错了什么提示?
答案 0 :(得分:1)
User.UserData = newUserData;
User.UserDataPrivate = newUserDataPrivate;
User.UserStatus = newUserStatus;
return User.save({ depth: 1 }).then (() => {
this.props.navigation.dispatch(createAccountResetStack)
});
这应该可以在没有单独保存的情况下工作。
答案 1 :(得分:0)
我认为我的问题是这些UserData,UserDataPrivate和UserStatus实际上引用了User而不是彼此。
这有效:
handleRegister = (event) => {
this.setState({ loading: true })
username = this.state.cellphone.replace(/\D/g,'');
db.ready().then((db) => {
var newUser = new db.User({
'username': username
});
db.User.register(newUser, this.state.password).then(User => {
console.log(db.me.id)
db.User.load(db.User.me.id).then(() => {
var newUserData = new db.UserData({
User: db.User.me,
});
var newUserDataPrivate = new db.UserDataPrivate({
User: db.User.me,
phone: username,
});
var newUserStatus = new db.UserStatus({
User: db.User.me,
status: 0,
licenseVerified: false,
backgroundChecked: false,
hourlyRate: 0,
lastUpdater: null,
});
newUserData.save()
newUserDataPrivate.save()
newUserStatus.save()
User.UserData = newUserData;
User.UserDataPrivate = newUserDataPrivate;
User.UserStatus = newUserStatus;
return User.update().then (() => {
this.props.navigation.dispatch(createAccountResetStack)
});
})
})
.catch(error => {
alert(error.message)
})
})
}