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

Save data to database in 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 地图 API.

中获取 lat/long

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

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

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

如果要修改现有对象,应该从数据库中获取现有对象,然后修改它。这可确保不会因覆盖而丢失任何更新。

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});