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

时间:2019-04-09 04:50:51

标签: node.js react-native

我有一个react native 0.59应用程序,它连接到nodejs服务器。该应用程序发起一个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中称为异步。这是代码:

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

  };

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

1 个答案:

答案 0 :(得分:2)

您的资源可能位于\x790111 1001

如果在登录时不尝试使用1xxx xxxx,则会看到实际信息的存储位置。

API响应始终由状态,标头,数据等组成。