Sequelize创建具有关联的实例

时间:2018-02-06 08:37:20

标签: node.js sequelize.js

在Sequelize 4中,有两个类:<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Sensemeister HVAC</title> <!-- Tell the browser to be responsive to screen width --> <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport"> <!-- Bootstrap 3.3.7 --> <link rel="stylesheet" href="css/bootstrap.min.css"> <!-- Theme style --> <link rel="stylesheet" href="css/AdminLTE.min.css"> <!-- AdminLTE Skins. Choose a skin from the css/skins folder instead of downloading all of them to reduce the load. --> <link rel="stylesheet" href="css/_all-skins.min.css"> <!-- Morris chart --> <!-- Google Font --> <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700,300italic,400italic,600italic"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="/socket.io/socket.io.js"></script> <script src="https://code.jquery.com/jquery-1.11.1.js"></script> <script> $(function () { var socketz = io(); $('form').submit(function(){ socketz.emit('emit_from_client', $('#sicaklikSet').val()); $('#sicaklikSet').val(''); return false; }); socketz.on('emit_from_client', function(data){ //var data = "ID101;Main;24.0;3276.0;3276.0;0;0;1;30.0;3;76.0;85.0;25.0*"; var tokens = data.toString().split(";"); tokens[tokens.length-1] = tokens[tokens.length-1].split("*",1); for(var i=2; i< tokens.length; ++i)//first two param not necessary { var idStr = 'dataToken' + i.toString(); document.getElementById(idStr).innerHTML = tokens[i]; } //$('#messages').text(data); }); }); $(document).ready(function(){ $("#btn1").click(function(){ $("#onOffButton").text("Hello world!"); }); $("#btn2").click(function(){ $("#test2").html("<b>Hello world!</b>"); }); $("#btn3").click(function(){ $("#test3").val("Dolly Duck"); }); }); </script> </head> </code> User,它们通过

关联
Profile

现在我有一个名为User.associate = (model) => { User.hasMany(model.profile); } 的用户实例,并希望创建user的实例,如何创建与Profile的关联。

这是我尝试过的。它有效,但我正在寻找更好的解决方案。

user

这是我改变的另一种方式,但不起作用:

var Profile = require('../models/profile');
Profile.create({userId: user.id})

有什么想法吗?

3 个答案:

答案 0 :(得分:0)

好吧,这有助于:

var profile = new Profile();
profile.setUser(user);
Profile.create(profile).then(res.send('success'));

答案 1 :(得分:0)

创建(或构建并保存)您的个人资料和用户实例。配置文件中的setUser。保存个人资料(再次)。

这样的事情:

const profile = Profile.build({...});
profile.save();

const user = User.build({...});
user.save();

profile.setUser(user);

profile.save();

在使用promises的实践中,在构建newUser和profile之后可能看起来更像是这样:

newUser.save().catch(err => console.log('Failed to create user', err))

profile.save()
.then(profile => profile.setUser(user))
.then(profile => profile.save())
.then(profile => {
  res.json(profile)
})
.catch(err => console.log('Failed to create profile', err));

答案 2 :(得分:0)

尝试一下

xpathRetweet = //div[@data-testid='retweet']//*[name()='svg']//*[name()='g']

然后

User.associate = (model) => {
    User.hasMany(model.profile, as "profile");
}