sas js并在neo4j中创建节点之间的关系

时间:2015-08-18 09:16:55

标签: neo4j sails.js cypher nodes

我有2个节点,第一个是用户,第二个是帖子。 我想创建用户中的Auser与帖子中的auserPost之间的关系。 我开始使用Sailsjs并在两个节点之间创建关系时遇到问题。我在neo4j的数据库。 任何想法?

其实我想将以下代码转换为sailsjs api:

   CREATE (matrix:Movie { title:"The Matrix",released:1997 })
   CREATE (cloudAtlas:Movie { title:"Cloud Atlas",released:2012 })
   CREATE (forrestGump:Movie { title:"Forrest Gump",released:1994 })
   CREATE (keanu:Person { name:"Keanu Reeves", born:1964 })
   CREATE (robert:Person { name:"Robert Zemeckis", born:1951 })
   CREATE (tom:Person { name:"Tom Hanks", born:1956 })
   CREATE (tom)-[:ACTED_IN { roles: ["Forrest"]}]->(forrestGump)
   CREATE (tom)-[:ACTED_IN { roles: ['Zachry']}]->(cloudAtlas)
   CREATE (robert)-[:DIRECTED]->(forrestGump)

1 个答案:

答案 0 :(得分:0)

将此代码添加到适配器

runQuery: function(connection , collection , myQuery, params , cb){
        query(connection, collection, myQuery, params, cb, true);
    }

并在你的控制器中使用:

getTestData:function(req,res){
var lang  = req.param("lang");
var skip = req.param("skip");
//get user id from session
var userId=req.session.userId;
var limit = 10;

//relations.posted is a string
var q = [
  'match (u:'+Users.tablename+')-[:'+relations.posted+']->(p:'+Posts.tablename+')-[:'+relations.posted_to+']->(h:'+Headlines.tablename+'),',
  '(h)-[:'+relations.cover_image+']->(i:'+Files.tablename+'),(p)-[:'+relations.sound_file+']->(s:'+Files.tablename+'),(hp:'+Posts.tablename+')-[:'+relations.posted_to+']->(h),',
  '(u)-[:'+relations.profile_picture+']->(ui:'+Files.tablename+')',
  'where p.isDeleted = false and hp.isDeleted = false',
  'return distinct(h) as headlines,count(hp) as postCount,max(hp.createdAt) as lastPostDate,hp as lastPost ,i as headlineImage,s as lastPostSound,u as user,ui as userProfileImg',
  'order by postCount desc',
  'skip '+skip,
  'limit '+limit
];
var params={};
Headlines.runQuery(q,params,function(err,result){
  var feeds = [];
  if(err == null){
    for (var i = 0; i < result.length; i++) {
      var obj = result[i];
      var feed = {};
      feed.post={
        id:obj.lastPost.id,
        uid:obj.lastPost.data.uid,
        url:obj.lastPostSound.data.url
      };

      feeds.push(feed);
    }
  }
  return res.json({
    err:err,
    result:feeds
  });
});
}