gpt4 book ai didi

javascript - Sequelize.js node.js 多对多关联

转载 作者:行者123 更新时间:2023-12-03 22:42:53 25 4
gpt4 key购买 nike

我正在使用 sequelize node.js 模块在 Postgres sql 中形成模式

我有以下两个项目模式和我的用户。

项目

module.exports = function(sequelize, DataTypes) {
var project = sequelize.define('project', {
description: DataTypes.STRING
},{
classMethods: {
associate: function(models) {
project.hasMany(models.myusers,{through:"project_persons"});
}
}
});

return project;
};

我的用户
    module.exports = function(sequelize, DataTypes) {
var myusers = sequelize.define('myusers', {
name: DataTypes.STRING
},{
classMethods: {
associate: function(models) {
myusers.hasMany(models.project,{through:"project_persons"});
}
}
});

return myusers;
};

我试图建立多对多的关系。当我运行应用程序第三个表“project_person 已创建”。

index.js(路线)我有以下代码
models.myusers.create({
description:"newuser"
}).success(function(user) {
var project1;
var project2;

models.project.create({
description:"p1"
}).success(function(p1){
console.log("Project 1 created");
project1 = p1;
});
models.project.create({
description:"p2"
}).success(function(p2){
console.log("Project 2 created");
project2 = p2;
});

user.setProjects([project1,project2]).success(function() {
console.log("CREATION SUCESS");
});
});

在屏幕上给我以下错误

/home/hassan/Desktop/node/node_modules/sequelize/lib/associations/has-many-double-linked.js:151 foreignIdentifier] = ((targetKeys.length === 1) ? unassociatedObject[targetKey ^ TypeError: Cannot read property 'id' of undefined at /home/hassan/Desktop/node/node_modules/sequelize/lib/associations/has-many-double-linked.js:151:88 at Array.map (native) at module.exports.HasManyDoubleLinked.injectSetter (/home/hassan/Desktop/node/node_modules/sequelize/lib/associations/has-many-double-linked.js:147:38) at instance.(anonymous function).success.proxy.events (/home/hassan/Desktop/node/node_modules/sequelize/lib/associations/has-many.js:317:39) at EventEmitter.emit (events.js:95:17) at module.exports.CustomEventEmitter.emit (/home/hassan/Desktop/node/node_modules/sequelize/lib/emitters/custom-event-emitter.js:107:33) at null. (/home/hassan/Desktop/node/node_modules/sequelize/lib/associations/has-many-double-linked.js:74:63) at EventEmitter.emit (events.js:95:17) at module.exports.CustomEventEmitter.emit (/home/hassan/Desktop/node/node_modules/sequelize/lib/emitters/custom-event-emitter.js:107:33) at emitter.query.success.error.proxy.events (/home/hassan/Desktop/node/node_modules/sequelize/lib/query-interface.js:890:19) 24 Apr 10:31:47 - [nodemon] app crashed - waiting for file changes before starting...



任何人都可以告诉我哪里出错了。

最佳答案

Sequelize 是一个异步库,因此您必须使用回调。这应该可以按您的预期工作:

models.myusers.create({ description: "newuser" }).success(function(user) {
models.project.create({ description: "p1" }).success(function(project1) {
console.log("Project 1 created");
models.project.create({ description: "p2" }).success(function(project2) {
console.log("Project 2 created");
user.setProjects([ project1, project2 ]).success(function() {
console.log("CREATION SUCESS");
});
});
});
});

更好的是:使用 complete()和正确的错误(如果有的话):
models.myusers.create({ description: "newuser" }).complete(function(err, user) {
if (err) throw err;
models.project.create({ description: "p1" }).complete(function(err, project1) {
if (err) throw err;
console.log("Project 1 created");
models.project.create({ description: "p2" }).complete(function(err, project2) {
if (err) throw err;
console.log("Project 2 created");
user.setProjects([ project1, project2 ]).complete(function(err, association) {
if (err) throw err;
console.log("CREATION SUCESS");
});
});
});
});

关于javascript - Sequelize.js node.js 多对多关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23276574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com