简体   繁体   English

#Sequelize在添加时检索所有属性

[英]#Sequelize while adding retrieve all attributes

I would like to find a way to retrieve all my attributes while inserting in my Database. 我想找到一种在插入数据库时​​检索所有属性的方法。

models.association.build(associationParams)
    .save()
    .then(function(assoAdded){
        return next(assoAdded);
    }).catch(function(err){
        // # TODO : implement error Handler
        return next(err);
    });

I got this : 我懂了 :

{
   "idAssoParente": null,
   "id": 420,
   "name": "a",
   "email": "aa@aa.aa",       
   "updated_at": "2015-07-29T17:12:47.000Z",
   "created_at": "2015-07-29T17:12:47.000Z"
}

But I want to return all my fields like description , phone , city from my database even if they are empty. 但是我想从我的数据库返回我的所有字段,例如description , phone , city ,即使它们为空。 Should I necessarily do a find after adding to get all my fields or does it exist a way to retrieve my fields without doing an other request ? 我应该在添加后获取所有字段吗?还是存在一种无需执行其他请求即可检索我的字段的方法? Thanks 谢谢

In short, yes you would need to query your db to return the info. 简而言之,是的,您将需要查询数据库以返回信息。 I just started using Sequelize but I found the following worked for me. 我刚开始使用Sequelize,但发现以下对我有用。

// if all the info you need is in your user  
Users.build({req.body})
        .save()
            .then(function(newUser){
               Users.find({where: {UserID: newUser.UserID}}).then(function(user){
                 //resolve your promise as you please.
               });

  // or if address info is in another model you can use eager loading.
  Users.build({req.body})
        .save()
            .then(function(newUser){
               Users.find({where: {UserID: newUser.UserID},
                                  include: [{
                                      model: address
                                  }]
                          }).then(function(user){
                 //resolve your promise as you please.
               });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM