首页 > 解决方案 > 为什么实时数据库不更新?

问题描述

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

这段代码在我的其他项目中有效

标签: typescriptfirebaseionic-frameworkfirebase-realtime-database

解决方案


没关系我得到它即使我不明白为什么

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

推荐阅读