javascript - How to remove attribute from response using sequelize? - Stack Overflow

const User = sequelize.define('user', { attributesfirstName: {type: Sequelize.STRING,allowN

const User = sequelize.define('user', {
    // attributes
    firstName: {
        type: Sequelize.STRING,
        allowNull: false
    },
    lastName: {
        type: Sequelize.STRING
        // allowNull defaults to true
    }
});

const Project = sequelize.define('project', {
    // attributes
    projectName: {
        type: Sequelize.STRING,
        allowNull: false
    }
});
User.belongsToMany(Project,{as:'Projects',through:'user_project'});
Project.belongsToMany(User,{as:'Users',through:'user_project'});

Fetching data like this

app.use('/get', (req, res) => {
    User.findAll({
        attributes: ['firstName'],
        include: [{
         model:Project,
            as:'Projects',
            attributes: ['projectName']
        }]}).then((result) => {
        res.json(result)
    })
})

getting a response like this

 [{
    "firstName": "naveen",
    "Projects": [
      {
        "projectName": "EV",
        "user_project": {
          "createdAt": "2019-07-21T06:17:49.119Z",
          "updatedAt": "2019-07-21T06:17:49.119Z",
          "userId": 1,
          "projectId": 3
        }
      }
    ]
  }]

Expected response

[{
    "firstName": "naveen",
    "Projects": [
      {
        "projectName": "EV",
      }
    ]
  }]

I am using sequelize in my project().I am fetching data from DB.I have many to many mapping I already add attributes property to remove unnecessary data. but it not works for me

const User = sequelize.define('user', {
    // attributes
    firstName: {
        type: Sequelize.STRING,
        allowNull: false
    },
    lastName: {
        type: Sequelize.STRING
        // allowNull defaults to true
    }
});

const Project = sequelize.define('project', {
    // attributes
    projectName: {
        type: Sequelize.STRING,
        allowNull: false
    }
});
User.belongsToMany(Project,{as:'Projects',through:'user_project'});
Project.belongsToMany(User,{as:'Users',through:'user_project'});

Fetching data like this

app.use('/get', (req, res) => {
    User.findAll({
        attributes: ['firstName'],
        include: [{
         model:Project,
            as:'Projects',
            attributes: ['projectName']
        }]}).then((result) => {
        res.json(result)
    })
})

getting a response like this

 [{
    "firstName": "naveen",
    "Projects": [
      {
        "projectName": "EV",
        "user_project": {
          "createdAt": "2019-07-21T06:17:49.119Z",
          "updatedAt": "2019-07-21T06:17:49.119Z",
          "userId": 1,
          "projectId": 3
        }
      }
    ]
  }]

Expected response

[{
    "firstName": "naveen",
    "Projects": [
      {
        "projectName": "EV",
      }
    ]
  }]

I am using sequelize in my project(http://docs.sequelizejs.).I am fetching data from DB.I have many to many mapping I already add attributes property to remove unnecessary data. but it not works for me

Share Improve this question edited Jul 21, 2019 at 11:22 Bahman Parsa Manesh 2,3683 gold badges18 silver badges36 bronze badges asked Jul 21, 2019 at 6:39 user944513user944513 12.8k52 gold badges185 silver badges348 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

You could use the options.include[].through.attributes to define fields selected from the relation table, so you could use:

User.findAll({
        attributes: ['firstName'],
        include: [{
         model:Project,
            as:'Projects',
            attributes: ['projectName'],
            through: { attributes: [] } // using empty array will cause not to return the relation fields at all
        }]}).then((result) => {
        res.json(result)
    })

Use delete key

    var YourJson = [{
    "firstName": "naveen",
    "Projects": [
      {
        "projectName": "EV",
        "user_project": {
          "createdAt": "2019-07-21T06:17:49.119Z",
          "updatedAt": "2019-07-21T06:17:49.119Z",
          "userId": 1,
          "projectId": 3
        }
      }
    ]
  }];

    delete YourJson['0']['Projects']['0']['user_project'];

    console.log(YourJson)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744276092a4566338.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信