为什么实时数据库不更新?

时间:2020-06-02 08:25:15

标签: typescript firebase ionic-framework firebase-realtime-database

   addDevice(deviceId: string, fireId: string){
    let Token: string;
    let UserId: string;
    let newDevice: Device;
    return this.authSrv.userId.pipe(take(1),
      switchMap(
        userId => {
          UserId = userId;
          return this.authSrv.token;
        }
      ),take(1),
      switchMap(
        token => {
          if(!UserId){
            throw new Error('No user id found');
          }
          Token = token;
          return this.http.get<DeviceData>(`https://....firebaseio.com/device/${fireId}.json? 
          auth=${token}`)
        }
      ),
      switchMap(
        deviceData => {
          if(!deviceData){
            throw new Error('No Device Id found or Pass Id incorrect');
          }
          newDevice = new Device(
              fireId, 
              deviceData.deviceId,
              deviceData.ver,
              deviceData.slot,
              UserId
          );
          this.http.put(`https://....firebaseio.com/device/${fireId}.json?auth=${Token}`,
            {...newDevice, id:null}
          );
          return this.devices;
        }
      )
    );
  }

“ get()”得到了我的数据,但是“ put()”对我的Firebase没有任何作用,也没有显示任何错误,我尝试使用“ console.log”查看数据输入。

我遵循此代码作为我的基础

 places => {
  const upPlaceIndex = places.findIndex(pl => pl.id == placeId);
  upPlace = [...places];
  const oldPlace = upPlace[upPlaceIndex];
  upPlace[upPlaceIndex] = new Place(.....);
  return this.http.put(`https://....firebaseio.com/offers-places/${placeId}.json? 
  auth=${fetchToken}`, 
  {...upPlace[upPlaceIndex], id: null}
 }

此代码可在我的其他项目中使用

1 个答案:

答案 0 :(得分:0)

没关系,即使我不明白为什么我也明白了

    switchMap(
        deviceData => {
          if(!deviceData){
            throw new Error('No Device Id found or Pass Id incorrect');
          }
          newDevice = new Device(
              fireId, 
              deviceData.deviceId,
              deviceData.ver,
              deviceData.slot,
              UserId
          );
          this.http.put(`https://....firebaseio.com/device/${fireId}.json?auth=${Token}`,
            {...newDevice, id:null}
          );
          return this.devices;
        }
      )

我只是将其更改为此

switchMap(
        deviceData => {
          if(!deviceData){
            throw new Error('No Device Id found or Pass Id incorrect');
          }
          newDevice = new Device(
              fireId, 
              deviceData.deviceId,
              deviceData.ver,
              deviceData.slot,
              UserId
          );
          return this.http.put(`https://....firebaseio.com/device/${fireId}.json?auth=${Token}`,
            {...newDevice, id:null}
          );
        }
      ),
     switchMap(
      data => {
        return this.devices;
      }
     )