【问题标题】:Why Real-time Database not updates?为什么实时数据库不更新?
【发布时间】:2020-09-20 16:10:44
【问题描述】:
   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}
 }

这段代码在我的其他项目中可以使用

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2021-05-11
      • 2020-07-25
      • 2016-12-12
      • 1970-01-01
      • 1970-01-01
      • 2012-02-05
      • 1970-01-01
      • 2017-01-23
      • 1970-01-01
      相关资源
      最近更新 更多