简体   繁体   中英

Sequelize: instance.get is not a function

I have this code that queries all the data under my feedback model

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

It gives me an error result.get is not a function . I don't know why I can't use the get function. But from my other query, i can use the get function. I don't know why this code give me an error.

EDIT:

The value of the result is

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

If I use result.dataValues or result.get('field') works. But result.get({plain: true}) doesn't work. I want to use the latter because that's what I declared from my GraphQLObjectTypes and it would be a too much of work if i'll change all its functions.

I want to know what caused this error, to get it to work.

EDIT (2): I discovered that from my front end, Apollo React causes the error. I don't know what is the logic to the error, but rearranging the fields does the trick.

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

Still don't know why rearranging the fields fixes it.

Another way of doing this is raw:true, nest:true :

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

set raw parameter as true or false. without this parameters some queries may be get error. in my app I set the {raw: true} in setting section. I set to true or false when trying to join models

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM