Advertisement
Guest User

Untitled

a guest
Apr 21st, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. /* jshint esnext: true, asi: true */
  2. var neo4j = require('node-neo4j')
  3.  
  4.  
  5. // Create the neo4J object
  6. var db = new neo4j(serverURI)
  7.  
  8. exports.addPerson = (body, callback) => {
  9.  
  10. if (body.skills) {
  11. var sentSkills = body.skills
  12. var arraySkills = sentSkills.split(',')
  13. }else {
  14. var sentSkills = []
  15. }
  16.  
  17. const sentName = body.name
  18. const sentEmail = body.email
  19. const sentUsername = body.username
  20. const sentPassword = body.password
  21. const lecturerStatus = body.lecturer
  22.  
  23. db.readNodesWithLabelsAndProperties('Person',{ email: sentEmail }, function (err, node) {
  24. if (err) {
  25. return console.log(err)
  26. }
  27. if (node.length > 0){
  28. // The user already exists
  29. callback({code:401,status:'failed',message:'Person already exsits with the name '+sentName,data:sentName})
  30. }
  31. else {
  32. // Insert new Person
  33.  
  34.  
  35. db.insertNode({
  36. name: sentName,
  37. email: sentEmail,
  38. skills: sentSkills,
  39. username: sentUsername,
  40. password: sentPassword,
  41. lecturer: lecturerStatus
  42. }, 'Person', function (err, node) {
  43. personNode = node
  44. if (err) {
  45. return console.log(err+1)
  46. }
  47. else {
  48.  
  49.  
  50. // I hate you for not working
  51. // The i = 0 variable is not accessible -> arraySkill[i]^.trim()
  52. // ERROR: cannot read property trim of undefined
  53. console.log("success")
  54. for (i = 0; i < arraySkills.length; i++){
  55. arraySkills = body.skills.split(',')
  56. db.cypherQuery("MATCH (s:Skill {name:'"+arraySkills[i].trim()+"'}) RETURN s", function(err, node){
  57. if (err){
  58. console.log("ERROR1")
  59. console.log(err)
  60. }
  61. else {
  62. console.log(arraySkills[0])
  63. if (node.data == '')
  64. {
  65. db.cypherQuery("CREATE (s:Skill {name:'"+arraySkills[i].trim()+"'}) RETURN s", function(err, node){
  66. if (err){
  67. console.log("ERROR2")
  68. console.log(err)
  69. }
  70. else {
  71. console.log(node)
  72. db.cypherQuery("MATCH (p:Person), (s:Skill) WHERE p.name = '"+sentName.trim()+"' AND s.name = '"+arraySkills[i].trim()+"' CREATE (p)-[r:knows]->(s) RETURN r", function(err, node){
  73. if (err){
  74. console.log("ERROR3")
  75. console.log(err)
  76. }
  77. else {
  78. console.log(node)
  79. console.log("Success")
  80. }
  81. })
  82. }
  83. })
  84. }
  85. }
  86. })
  87. };
  88.  
  89. }
  90.  
  91.  
  92. })
  93.  
  94.  
  95.  
  96. // Output node data.
  97. callback({code:201,status:'success',message:'Person Added In '+sentName+' found...',data:node})
  98.  
  99. }
  100. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement