相关疑难解决方法(0)

Sails JS Waterline连接多个模型

嗨我正在尝试使用populate方法连接多个表,我用Google搜索并找不到有效的方法,我不想查询db几次来构建结果,是否可以用sails版本来解决它" ~0.10.0-rc7"我正在建造退出大项目,有超过一百个桌子.

var co = {
    adapter: 'someMysqlServer',
    migrate:'safe',
    autoCreatedAt: false,
    autoUpdatedAt: false,
    autoPK:false,
    attributes:{
        id:{
            type:"int",
            primaryKey: true,
            autoIncrement: true
        },
        code:"string",
        priority :"int",
        co_group_c_id :"int",
        timezone_c_id :"int",
        lang_c_id :"int",
        currency_c_id :"int",
        name_used :"string",
        name_official :"string",
        co_tax_no :"int",
        co_vat_no :"int",
        co_vat_reg :"int",
        co_reg_record :"string",
        co_representative :"string",
        co_addresses:{
            collection: "co_address",
            via: "co_user_id"
        },
    }
};
module.exports = co;

var co_address = {
    adapter: 'someMysqlServer',
    migrate:'safe',
    autoCreatedAt: false,
    autoUpdatedAt: false,
    autoPK:false,
    attributes: {
        id:{
            type:"int",
            primaryKey: true,
            autoIncrement: true,
        },
        address_c_id:"int" …
Run Code Online (Sandbox Code Playgroud)

node.js express sails.js waterline

7
推荐指数
1
解决办法
1万
查看次数

Sails.js使用Callback中的值设置模型值

我需要在我的模型中提供类似关联的东西.所以我有一个带有用户ID的名为Posts的模型,想要从该用户名获取用户名并显示它.

所以我的ForumPosts.js模型如下所示:

module.exports = {

  schema: true,

  attributes: {

    content: {
        type: 'text',
        required: true
    },

    forumTopicId: {
        type: 'text',
        required: true
    },

    userId: {
      type: 'integer',
      required: true
    },

    getUsername: function(){
      User.findOne(this.userId, function foundUser(err, user) {
        var username =  user.username;
      });
      console.log(username);
      return username;
    }
  }

};
Run Code Online (Sandbox Code Playgroud)

我知道这个返回不起作用,因为它是异步的...但是我如何在我的视图中显示它?在片刻,我用以下方法追溯价值:

<%= forumPost.getUsername() %>
Run Code Online (Sandbox Code Playgroud)

并且肯定得到一个不确定的回报......

所以问题是:我怎样才能返回这个值 - 或者是否有比实例模型更好的解决方案?

提前致谢!

javascript asynchronous node.js sails.js

2
推荐指数
1
解决办法
2485
查看次数