【问题标题】:Sequelize: instance.get is not a function续集:instance.get 不是函数
【发布时间】:2019-12-20 06:18:04
【问题描述】:

我有这段代码可以查询我的反馈模型下的所有数据

const result = await models.Feedback.findOne({ 
          include: [{
            association: models.Feedback.Point,
            order: [["date_saved", "DESC"]],
            // where: { version_id: { [Op.not]: null } },
            include: [{ 
              association: models.Point.Creator 
            }, { 
              association: models.Point.User,
            }, { 
              association: models.Point.Comment,
            }]
          }, {
            association: models.Feedback.Project,
            where: { subscription_id: currentUser.subscription_id }
          }, {
            association: models.Feedback.User,
            include: [{
              association: models.ProjectTeam.User
            }]
          }],
          where: { id },
        });
console.log(result.get({plain:true})'

它给了我一个错误result.get is not a function。我不知道为什么我不能使用 get 功能。但是从我的其他查询中,我可以使用 get 函数。我不知道为什么这段代码给我一个错误。

编辑:

结果的值为

ep_tblfeedback {
  dataValues:
   { id: 239,
     proj_id: 20006,
     title: 'sample',
     status: 'Open',
     date_saved: 2019-08-07T09:41:16.000Z,
     title_trans: null,
     Points: [ [ep_tblpoint] ],
     Project:
      ep_tblproject {
        dataValues: [Object],
        _previousDataValues: [Object],
        _changed: {},
        _modelOptions: [Object],
        _options: [Object],
        isNewRecord: false },
     Users:
      [ [ep_tblproject_team],
        [ep_tblproject_team],
        [ep_tblproject_team],
        [ep_tblproject_team],
        [ep_tblproject_team],
        [ep_tblproject_team] ] },
  _previousDataValues:
   { id: 239,
     proj_id: 20006,
     title: 'sample',
     status: 'Open',
     date_saved: 2019-08-07T09:41:16.000Z,
     title_trans: null,
     Points: [ [ep_tblpoint] ],
     Project:
      ep_tblproject {
        dataValues: [Object],
        _previousDataValues: [Object],
        _changed: {},
        _modelOptions: [Object],
        _options: [Object],
        isNewRecord: false },
     Users:
      [ [ep_tblproject_team],
        [ep_tblproject_team],
        [ep_tblproject_team],
        [ep_tblproject_team],
        [ep_tblproject_team],
        [ep_tblproject_team] ] },
  _changed: {},
  _modelOptions:
   { timestamps: false,
     validate: {},
     freezeTableName: false,
     underscored: false,
     paranoid: false,
     rejectOnEmpty: false,
     whereCollection: { id: 239 },
     schema: null,
     schemaDelimiter: '',
     defaultScope: {},
     scopes: {},
     indexes: [],
     name: { plural: 'ep_tblfeedbacks', singular: 'ep_tblfeedback' },
     omitNull: false,
     tableName: 'ep_tblfeedback',
     sequelize:
      Sequelize {
        options: [Object],
        config: [Object],
        dialect: [MysqlDialect],
        queryInterface: [QueryInterface],
        models: [Object],
        modelManager: [ModelManager],
        connectionManager: [ConnectionManager],
        importCache: [Object] },
     hooks: {} },
  _options:
   { isNewRecord: false,
     _schema: null,
     _schemaDelimiter: '',
     include: [ [Object], [Object], [Object] ],
     includeNames: [ 'Points', 'Project', 'Users' ],
     includeMap: { Points: [Object], Project: [Object], Users: [Object] },
     includeValidated: true,
     attributes:
      [ 'id', 'proj_id', 'title', 'status', 'date_saved', 'title_trans' ],
     raw: true },
  isNewRecord: false,
  Points:
   [ ep_tblpoint {
       dataValues: [Object],
       _previousDataValues: [Object],
       _changed: {},
       _modelOptions: [Object],
       _options: [Object],
       isNewRecord: false,
       Creator: [ep_tblusers],
       Users: [Array],
       comments: [Array] } ],
  Project:
   ep_tblproject {
     dataValues:
      { id: 20006,
        proj_name: 'exactplace',
        client_id: 291,
        proj_nr: 'ep-01',
        subscription_id: 1000,
        status: 'Open' },
     _previousDataValues:
      { id: 20006,
        proj_name: 'exactplace',
        client_id: 291,
        proj_nr: 'ep-01',
        subscription_id: 1000,
        status: 'Open' },
     _changed: {},
     _modelOptions:
      { timestamps: false,
        validate: {},
        freezeTableName: false,
        underscored: false,
        paranoid: false,
        rejectOnEmpty: false,
        whereCollection: [Object],
        schema: null,
        schemaDelimiter: '',
        defaultScope: {},
        scopes: {},
        indexes: [],
        name: [Object],
        omitNull: false,
        tableName: 'ep_tblproject',
        sequelize: [Sequelize],
        hooks: {} },
     _options:
      { isNewRecord: false,
        _schema: null,
        _schemaDelimiter: '',
        include: undefined,
        includeNames: undefined,
        includeMap: undefined,
        includeValidated: true,
        raw: true,
        attributes: undefined },
     isNewRecord: false },
  Users:
   [ ep_tblproject_team {
       dataValues: [Object],
       _previousDataValues: [Object],
       _changed: {},
       _modelOptions: [Object],
       _options: [Object],
       isNewRecord: false,
       ep_tbluser: [ep_tblusers] },
     ep_tblproject_team {
       dataValues: [Object],
       _previousDataValues: [Object],
       _changed: {},
       _modelOptions: [Object],
       _options: [Object],
       isNewRecord: false,
       ep_tbluser: [ep_tblusers] },
     ep_tblproject_team {
       dataValues: [Object],
       _previousDataValues: [Object],
       _changed: {},
       _modelOptions: [Object],
       _options: [Object],
       isNewRecord: false,
       ep_tbluser: [ep_tblusers] },
     ep_tblproject_team {
       dataValues: [Object],
       _previousDataValues: [Object],
       _changed: {},
       _modelOptions: [Object],
       _options: [Object],
       isNewRecord: false,
       ep_tbluser: [ep_tblusers] },
     ep_tblproject_team {
       dataValues: [Object],
       _previousDataValues: [Object],
       _changed: {},
       _modelOptions: [Object],
       _options: [Object],
       isNewRecord: false,
       ep_tbluser: [ep_tblusers] },
     ep_tblproject_team {
       dataValues: [Object],
       _previousDataValues: [Object],
       _changed: {},
       _modelOptions: [Object],
       _options: [Object],
       isNewRecord: false,
       ep_tbluser: [ep_tblusers] } ] }

如果我使用 result.dataValues 或 result.get('field') 有效。但是 result.get({plain: true}) 不起作用。我想使用后者,因为这是我从 GraphQLObjectTypes 中声明的,如果我要更改它的所有功能,那将是一项太多的工作。

我想知道是什么导致了这个错误,让它工作。

编辑(2): 我从我的前端发现,Apollo React 会导致错误。我不知道错误的逻辑是什么,但是重新排列字段就可以了。

export const FEEDBACK_QUERY = (id) => gql`
  {
    feedback (id: ${id}) {
      id
      title
      proj_id
      proj_name
      clientname
      date_saved
      versions {
        id
        version_name
        __typename @include(if: false)
      }
      points {
        id
        point_nr
        version_name
        time_approve
        time_estimate
        priority
        team {
          id 
          name
          project_role
          __typename @include(if: false)
        }
        instructions
        inst_engtrans
        status
        deadline
        creator
        comments {
          id
          user_id
          comments
          date_commented
          __typename @include(if: false)
        }
        ack
        date
        __typename @include(if: false)
      }
      team {
        id
        name
        email
        project_role
        __typename @include(if: false)
      }
      __typename @include(if: false)
    }
  } 
`;

仍然不知道为什么重新排列字段可以解决问题。

【问题讨论】:

    标签: javascript graphql sequelize.js


    【解决方案1】:

    另一种方法是raw:true, nest:true:

    const result = await models.Feedback.findOne({
        include: [
            ...
        ],
        where: {
            ...
        },
        raw : true ,
        nest : true
    });
    

    【讨论】:

    • 结果不为空,我可以使用 .get('field') 或 .dataValues 但不能使用 .get({plain: true})
    • @Syther,如果您想以其他方式进行操作,请查看更新后的答案。
    • 我想我别无选择,只能更改我的所有代码。但是你知道为什么它不允许我使用 get({plain:true}) 函数吗?它适用于其他结果,但这个具有许多关联的结果显示错误。
    • @Syther,让我调查一下并尽快回复您。
    • 刚刚意识到,我无法将其设置为原始。我需要虚拟吸气剂。
    【解决方案2】:

    将原始参数设置为真或假。 如果没有这个参数,一些查询可能会出错。 在我的应用程序中,我在设置部分设置了 {raw: true}。 我在尝试加入模型时设置为 true 或 false

    【讨论】:

    • 您能否详细说明您的回答?
    • 这个答案和另一个答案一样。另外我当时需要使用虚拟吸气剂,所以将其设置为 raw 不是选项。
    猜你喜欢
    • 2018-05-12
    • 2017-05-25
    • 2018-05-21
    • 2020-06-11
    • 1970-01-01
    • 1970-01-01
    • 2016-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多