node.js - Sails.js Same Model 多对多关联

标签 node.js model sails.js waterline

Sails.js .10 rc8

我已经完全没有这个想法了

我有一个名为 User 的模型,我想将它在一个集合中与其他用户相关联,例如 friend 列表。就像一个多对多的关联

   //User.js
   friends: {
     collection: 'user',
     via: 'friends'
   }

但是当我运行时 .populate(' friend ') 它不填充任何内容。有什么想法吗?

最佳答案

我发现实现这一点的最佳方法实际上是添加对 id 的引用。

检查此用户模型:

module.exports = {
  attributes: {
    name: {
      type: 'string',
      required: true,
      minLength: 2
    },
    email: {
      type: 'email',
      required: true,
      unique: true
    },
    friends: {
      collection: 'user',
      via: 'id'
    }
  }
};

现在,如果您运行 sails 控制台,您可以测试以下命令:

User.create({name:'test',email:'test@test.com'}).exec(console.log);

返回:

{ name: 'test',
  email: 'test@test.com',
  createdAt: '2016-12-01T22:06:19.723Z',
  updatedAt: '2016-12-01T22:06:19.723Z',
  id: 1 }

您创建了第一个用户。让我们创建一些其他的:

User.create({name:'test2',email:'test2@test.com'}).exec(console.log);

导致:

 { name: 'test2',
  email: 'test2@test.com',
  createdAt: '2016-12-01T22:06:40.808Z',
  updatedAt: '2016-12-01T22:06:40.808Z',
  id: 2 }

让我们看看到目前为止我们有什么:

User.find().populate('friends').exec(console.log);

[ { friends: [],
    name: 'test',
    email: 'test@test.com',
    createdAt: '2016-12-01T22:06:19.723Z',
    updatedAt: '2016-12-01T22:06:19.723Z',
    id: 1 },
  { friends: [],
    name: 'test2',
    email: 'test2@test.com',
    createdAt: '2016-12-01T22:06:40.808Z',
    updatedAt: '2016-12-01T22:06:40.808Z',
    id: 2 } ]

现在,让我们使用对第一个用户的引用来创建一个有 friend 的用户。请注意,我只传递了一个 id,而不是数组:

User.create({name:'test3',email:'test3@test.com', friends:1}).exec(console.log);

现在,结果就是这个。请注意,“ friend ”没有出现。这是设计使然。

{ name: 'test3',
  email: 'test3@test.com',
  createdAt: '2016-12-01T22:07:34.988Z',
  updatedAt: '2016-12-01T22:07:34.994Z',
  id: 3 }

让我们用 populate 进行查找以查看当前状态:

User.find().populate('friends').exec(console.log);

现在我们看到第三个用户有 friend 了。其他的有一个空数组。

 [ { friends: [],
    name: 'test',
    email: 'test@test.com',
    createdAt: '2016-12-01T22:06:19.723Z',
    updatedAt: '2016-12-01T22:06:19.723Z',
    id: 1 },
  { friends: [],
    name: 'test2',
    email: 'test2@test.com',
    createdAt: '2016-12-01T22:06:40.808Z',
    updatedAt: '2016-12-01T22:06:40.808Z',
    id: 2 },
  { friends: 
     [ { name: 'test',
         email: 'test@test.com',
         createdAt: '2016-12-01T22:06:19.723Z',
         updatedAt: '2016-12-01T22:06:19.723Z',
         id: 1 } ],
    name: 'test3',
    email: 'test3@test.com',
    createdAt: '2016-12-01T22:07:34.988Z',
    updatedAt: '2016-12-01T22:07:34.994Z',
    id: 3 } ]

让我们创建第四个,这次和两个 friend 一起:

User.create({name:'test4',email:'test4@test.com', friends:[1,2]}).exec(console.log);

导致(同样,没有 friend 属性):

 { name: 'test4',
  email: 'test4@test.com',
  createdAt: '2016-12-01T22:07:50.539Z',
  updatedAt: '2016-12-01T22:07:50.542Z',
  id: 4 }

这一次,我们将一个 id 数组传递给 friends。让我们看看目前的状态:

User.find().populate('friends').exec(console.log);

 [ { friends: [],
    name: 'test',
    email: 'test@test.com',
    createdAt: '2016-12-01T22:06:19.723Z',
    updatedAt: '2016-12-01T22:06:19.723Z',
    id: 1 },
  { friends: [],
    name: 'test2',
    email: 'test2@test.com',
    createdAt: '2016-12-01T22:06:40.808Z',
    updatedAt: '2016-12-01T22:06:40.808Z',
    id: 2 },
  { friends: 
     [ { name: 'test',
         email: 'test@test.com',
         createdAt: '2016-12-01T22:06:19.723Z',
         updatedAt: '2016-12-01T22:06:19.723Z',
         id: 1 } ],
    name: 'test3',
    email: 'test3@test.com',
    createdAt: '2016-12-01T22:07:34.988Z',
    updatedAt: '2016-12-01T22:07:34.994Z',
    id: 3 },
  { friends: 
     [ { name: 'test',
         email: 'test@test.com',
         createdAt: '2016-12-01T22:06:19.723Z',
         updatedAt: '2016-12-01T22:06:19.723Z',
         id: 1 },
       { name: 'test2',
         email: 'test2@test.com',
         createdAt: '2016-12-01T22:06:40.808Z',
         updatedAt: '2016-12-01T22:06:40.808Z',
         id: 2 } ],
    name: 'test4',
    email: 'test4@test.com',
    createdAt: '2016-12-01T22:07:50.539Z',
    updatedAt: '2016-12-01T22:07:50.542Z',
    id: 4 } ]

现在,如何向现有用户添加 friend ?这有点奇怪:您必须首先 find 用户,然后在生成的模型上使用 add 函数,然后,save 它。在我的日常代码中,我有一个辅助函数可以做到这一点。所以,这是一个例子:

User.findOne(1).populate('friends').exec(function(err,u){ u.friends.add(3);u.save(function(err){ if(err) console.error(err);});});

现在在控制台中我们看不到任何结果。 现在让我们检查一下用户内容:

User.find().populate('friends').exec(console.log);

[ { friends: 
     [ { name: 'test3',
         email: 'test3@test.com',
         createdAt: '2016-12-01T22:07:34.988Z',
         updatedAt: '2016-12-01T22:07:34.994Z',
         id: 3 } ],
    name: 'test',
    email: 'test@test.com',
    createdAt: '2016-12-01T22:06:19.723Z',
    updatedAt: '2016-12-01T22:09:41.410Z',
    id: 1 },
  { friends: [],
    name: 'test2',
    email: 'test2@test.com',
    createdAt: '2016-12-01T22:06:40.808Z',
    updatedAt: '2016-12-01T22:06:40.808Z',
    id: 2 },
  { friends: 
     [ { name: 'test',
         email: 'test@test.com',
         createdAt: '2016-12-01T22:06:19.723Z',
         updatedAt: '2016-12-01T22:09:41.410Z',
         id: 1 } ],
    name: 'test3',
    email: 'test3@test.com',
    createdAt: '2016-12-01T22:07:34.988Z',
    updatedAt: '2016-12-01T22:07:34.994Z',
    id: 3 },
  { friends: 
     [ { name: 'test',
         email: 'test@test.com',
         createdAt: '2016-12-01T22:06:19.723Z',
         updatedAt: '2016-12-01T22:09:41.410Z',
         id: 1 },
       { name: 'test2',
         email: 'test2@test.com',
         createdAt: '2016-12-01T22:06:40.808Z',
         updatedAt: '2016-12-01T22:06:40.808Z',
         id: 2 } ],
    name: 'test4',
    email: 'test4@test.com',
    createdAt: '2016-12-01T22:07:50.539Z',
    updatedAt: '2016-12-01T22:07:50.542Z',
    id: 4 } ]

使用此方法,您甚至可以将同一用户添加到好友收藏中!

User.findOne(1).populate('friends').exec(function(err,u){ u.friends.add(1);u.save(function(err){ if(err) console.error(err);});});

玩得开心!

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

相关文章:

javascript - 如何在 Mongoose 中执行查询?

node.js - 无法使用 Socket.io 启动 NodeJS 服务器

data-binding - 直接在 View 中绑定(bind)模型属性

ruby-on-rails - 访问子模型 rails 中的父属性

node.js - Babel 无法使用 Mocha 并启动 Node 服务器

javascript - 为什么 Node.js HTTP 服务器不响应来自 Python 的请求?

ruby-on-rails-3 - 在Rails 3中复制记录

sails.js - Sails JS 复杂的多对多关联

node.js - Swig 模板引擎和 Sails.js - 如何更改 tagControls

javascript - 控制 console.log 的函数而不返回