从后端服务器返回的对象与在应用程序中收到的对象不同

问题描述 投票:0回答:1

我有一个连接到nodejs服务器的react native 0.59应用程序。应用程序启动fetch请求以从后端服务器检索当前用户对象。以下是处理服务器上的获取请求的代码:

router.get('/myself', [auth_deviceid, auth_userinfo], async (req, res) => {
    console.log("myself req.user :", req.user);
    return res.status(200).send(req.user);
});

这是上面的console.log输出。返回的用户对象如下:

user {
  dataValues:
   { id: 1,
     name: 'test uer1',
     email: null,
     cell: '123456789',
     cell_country_code: '1',
     comp_name: null,
     status: 'active',
     role: 'admin',
     device_id: '02f7e7aa907a2a2b',
     user_data: { user_secret: '' },
     last_updated_by_id: null,
     fort_token: '12345678901234567890',
     createdAt: 2019-04-06T07:00:00.000Z,
     updatedAt: 2019-04-06T07:00:00.000Z },
  _previousDataValues:
   { id: 1,
     name: 'test uer1',
     email: null,
     cell: '123456789',
     cell_country_code: '1',
     comp_name: null,
     status: 'active',
     role: 'admin',
     device_id: '02f7e7aa907a2a2b',
     user_data: { user_secret: '' },
     last_updated_by_id: null,
     fort_token: '12345678901234567890',
     createdAt: 2019-04-06T07:00:00.000Z,
     updatedAt: 2019-04-06T07:00:00.000Z },
  _changed: {},
  _modelOptions:
   { timestamps: true,
     validate: { custom_validate: [AsyncFunction: custom_validate] },
     freezeTableName: false,
     underscored: false,
     underscoredAll: false,
     paranoid: false,
     rejectOnEmpty: false,
     whereCollection: { id: '1' },
     schema: null,
     schemaDelimiter: '',
     defaultScope: {},
     scopes: [],
     indexes: [],
     name: { plural: 'users', singular: 'user' },
     omitNull: false,
     sequelize:
      Sequelize {
        options: [Object],
        config: [Object],
        dialect: [PostgresDialect],
        queryInterface: [QueryInterface],
        models: [Object],
        modelManager: [ModelManager],
        connectionManager: [ConnectionManager],
        importCache: {},
        test: [Object] },
     hooks: {},
     uniqueKeys: {} },
  _options:
   { isNewRecord: false,
     _schema: null,
     _schemaDelimiter: '',
     raw: true,
     attributes:
      [ 'id',
        'name',
        'email',
        'cell',
        'cell_country_code',
        'comp_name',
        'status',
        'role',
        'device_id',
        'user_data',
        'last_updated_by_id',
        'fort_token',
        'createdAt',
        'updatedAt' ] },
  __eagerlyLoadedAssociations: [],
  isNewRecord: false }

然而,在应用程序端收到完全不同的用户对象。这是控制台输出:

 'User returned in getuser as: %o', { type: 'default',
04-08 21:21:26.487 12710 12766 I ReactNativeJS:   status: 200,
04-08 21:21:26.487 12710 12766 I ReactNativeJS:   ok: true,
04-08 21:21:26.487 12710 12766 I ReactNativeJS:   statusText: undefined,
04-08 21:21:26.487 12710 12766 I ReactNativeJS:   headers:
04-08 21:21:26.487 12710 12766 I ReactNativeJS:    { map:
04-08 21:21:26.487 12710 12766 I ReactNativeJS:       { date: 'Tue, 09 Apr 2019 04:21:26 GMT',
04-08 21:21:26.487 12710 12766 I ReactNativeJS:         etag: 'W/"14f-/XZLqbaIchn159DwAYBdoQySOWg"',
04-08 21:21:26.487 12710 12766 I ReactNativeJS:         'x-powered-by': 'Express',
04-08 21:21:26.487 12710 12766 I ReactNativeJS:         connection: 'keep-alive',
04-08 21:21:26.487 12710 12766 I ReactNativeJS:         'content-length': '335',
04-08 21:21:26.487 12710 12766 I ReactNativeJS:         'content-type': 'application/json; charset=utf-8' } },
04-08 21:21:26.487 12710 12766 I ReactNativeJS:   url: 'http://192.168.2.133:3000/api/users/myself?_device_id=02f7e7aa907a2a2b',
04-08 21:21:26.487 12710 12766 I ReactNativeJS:   _bodyInit:
04-08 21:21:26.487 12710 12766 I ReactNativeJS:    { _data:
04-08 21:21:26.487 12710 12766 I ReactNativeJS:       { size: 335,
04-08 21:21:26.487 12710 12766 I ReactNativeJS:         offset: 0,
04-08 21:21:26.487 12710 12766 I ReactNativeJS:         blobId: '34fa1356-5846-4577-b4fa-77c99248246c' } },
04-08 21:21:26.487 12710 12766 I ReactNativeJS:   _bodyBlob:
04-08 21:21:26.487 12710 12766 I ReactNativeJS:    { _data:
04-08 21:21:26.487 12710 12766 I ReactNativeJS:       { size: 335,
04-08 21:21:26.487 12710 12766 I ReactNativeJS:         offset: 0,
04-08 21:21:26.487 12710 12766 I ReactNativeJS:         blobId: '34fa1356-5846-4577-b4fa-77c99248246c' } } }

_getUser()componentDidMount被称为async。这是代码:

module.exports._getUser = async () => {
    try {
      let url = `http://192.168.2.133:3000/api/users/myself?_device_id=${encodeURIComponent(DeviceInfo.getUniqueID())}`;
      console.log("getuser url : %s", url);
      let res = await fetch(url, {
        method: "GET",
         headers: {
          "x-auth-token": "mytoken", 
        }
      });
      console.log("User returned in getuser as: %o",  res); 
      return res;

    }
    catch (e) {
      console.log("Error in get myself : ", e);
      return "";
    };

  };

为什么在应用程序端没有收到用户对象?

node.js react-native
1个回答
2
投票

你的资源可能在res.datares.data.data上有售。

如果您在登录时没有尝试JSON.stringify(res),您将看到存储实际信息的位置。

API响应始终由状态,标题,数据等组成......

© www.soinside.com 2019 - 2024. All rights reserved.