如何让 Deep Saving 发挥作用?

How to get Deep Saving to work?

我很难像这里描述的那样深入储蓄工作: http://www.baqend.com/guide/topics/deep-loading/#deep-saving

我想创建一个用户,然后立即为将来的数据生成一些占位符对象,然后将这些新创建的引用保存回用户 table。

这是我的 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

关于我在这里做错了什么的提示?

我认为我的问题是这些 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)
      })
    })
  }
User.UserData = newUserData;
User.UserDataPrivate = newUserDataPrivate;
User.UserStatus = newUserStatus;
return User.save({ depth: 1 }).then (() => {
    this.props.navigation.dispatch(createAccountResetStack)
});

这应该可以在没有单独保存的情况下工作。