Creating instance with an association in Sequelize

后端 未结 8 1142
清酒与你
清酒与你 2021-02-02 09:09

Using Sequelize, I\'ve created two models: User and Login.

Users can have more than one Login, but a login must have exactly one user, which me

8条回答
  •  南旧
    南旧 (楼主)
    2021-02-02 09:43

    In your .then, the callback receives the model instance created by the previous call. You need to specify the argument inside the callback function.

    var user = User.create().then(function(user) {
    
      // THIS IS WHERE I WOULD LIKE TO SET THE ASSOCIATION
      var login = Login.create({
        userId: user.get('id')
      });
    
      return login
    
    }).then(function(login) {
        // all creation are complete. do something.
    });
    

    Also something important I would like to point out is your missing var statements! Those are important but not related to this question. See Declaring variables without var keyword

提交回复
热议问题