Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从SequelizeJS中的表中选择特定属性_Javascript_Mysql_Node.js_Orm_Sequelize.js - Fatal编程技术网

Javascript 从SequelizeJS中的表中选择特定属性

Javascript 从SequelizeJS中的表中选择特定属性,javascript,mysql,node.js,orm,sequelize.js,Javascript,Mysql,Node.js,Orm,Sequelize.js,这些是我的表(不包括所有列)和关系 var client = schema.define('client', { name: { type: Sequelize.STRING, allowNull: false }, } var task = schema.define('task', { name: { type: Sequelize.STRING, unique: true, allow

这些是我的表(不包括所有列)和关系

var client = schema.define('client', {
    name: {
        type: Sequelize.STRING,
        allowNull: false
    },
}

var task = schema.define('task', {
    name: {
        type: Sequelize.STRING,
        unique: true,
        allowNull: false
    },
    description: {
        type: Sequelize.STRING,
    }
}

var clientTask = schema.define('clientTask', {
    value: {
        type: Sequelize.STRING,
        allowNull: false,
        defaultValue: false
    },
}

client.belongsToMany(task, { through: clientTask });
task.belongsToMany(client, { through: clientTask });
我只想从task中获取
名称
,从clientTask中获取
,我按客户端id搜索,下面是我迄今为止尝试的内容

client.findAll({ 
    attributes: [], 
    where: {id: clientId}, 
    include: [{ 
        model: task, 
        attributes: ['name'] 
    }]
}).then(function (clients) { 
    //client.tasks is array with task objects(models) with only name attribute
    //client.tasks[0].clientTask is object(models) with all attributes but I want only `value`
}
基本上我想要的是这个查询

Select
  tasks.name,
  clienttasks.value
From
  clients Inner Join
  clienttasks
    On clienttasks.clientId = clients.id Inner Join
  tasks
    On clienttasks.taskId = tasks.id
Where clients.id = ?

你可以这样问它

clients.findById(1, {
    attributes: ['id'],
    include: [{
        model: tasks,
        attributes: ['name'],
        required: false
    }]
}).then(function(client) {
    return client.tasks.map(function(task) {
        return {
            name: task.name,
            value: task.clients_tasks.value
        };
    });
}).then(function(result) {
    console.log(result);
    // The rest of you logic here...
});

你可以这样问它

clients.findById(1, {
    attributes: ['id'],
    include: [{
        model: tasks,
        attributes: ['name'],
        required: false
    }]
}).then(function(client) {
    return client.tasks.map(function(task) {
        return {
            name: task.name,
            value: task.clients_tasks.value
        };
    });
}).then(function(result) {
    console.log(result);
    // The rest of you logic here...
});