将数据保存到Baqend的数据库中

时间:2017-09-27 19:46:26

标签: baqend

我似乎无法弄清楚如何更新数据库中的数据:

以下是代码:

  handleSubmit(event) {
    event.preventDefault();
    var fulladdress = this.state.address1 +" "+ this.state.city +", "+ this.state.state +" "+ this.state.zip;
    Geocoder.getFromLocation(fulladdress).then(
      json => {
        var location = json.results[0].geometry.location;
        //alert(location.lat + ", " + location.lng);
        var geo = new db.GeoPoint(location.lat, location.lng)
        var updatecompany = new db.Companies({
          id: '16747fce-f0b1-422b-aa60-220c2dccac58',
          name: this.state.name,
          logo: '',
          photo: '',
          address1: this.state.address1,
          address2: this.state.address2,
          city: this.state.city,
          state: this.state.state,
          zip: this.state.zip,
          geo: geo,
        });

        updatecompany.update().then(() => {
          alert("company saved!")
        })
      },
      error => {
        alert(error);
      }
    );
  }

在此文件中,我希望更新公司资料的数据。首先,我们从Google Maps API获取lat / long。

当我运行此操作时,我收到此错误:未处理的拒绝(EntityExistsError):实体/ db / Companies / 16747fce-f0b1-422b-aa60-220c2dccac58由不同的数据库管理。

我尝试删除ID,但后来我收到此错误:未处理的拒绝(PersistentError):无法插入新对象。

我是否需要使用密钥或其他东西?或者id是在对象之外传递还是什么?

1 个答案:

答案 0 :(得分:1)

如果要修改现有对象,则应从db获取现有对象,然后对其进行修改。这可确保覆盖不会丢失任何更新。

db.Companies.load(id).then(company => {
     company.name = this.state.name;
     ...
     return company.update();
});

或者,您只需获取托管数据库对象即可应用更改并强制保存。这将覆盖任何现有的数据库状态,只保存您的更改。

//This will get the current managed object for the Id
//That object may not have loaded state
company = db.Companies.ref(id);
company.name = this.state.name;
//The force overwrites any previously stored state
//This may result in lost updates when the object is concurrently saved by different clients
company.save({force: true});