2017-03-04 21 views
1

我試圖建立一個博客網絡應用程序作爲練習來了解特別是ORM和Sequelize。我很容易在帖子和評論以及用戶和帖子之間形成關係。那裏沒有問題。當我嘗試將用戶作爲關注者和用戶關聯到用戶時,恐怖開始了。我也希望它有可能跟隨你自己。許多很多自我參考在推文中像setwelize網絡應用程序

我已經試過這樣的事情:

const User = sequelize.define('user', { 
    firstName: { 
     type: Sequelize.STRING 
    }, 
    lastName: { 
     type: Sequelize.STRING 
    }, 
    email: { 
     type: Sequelize.STRING 
    }, 
    password: { 
     type: Sequelize.STRING 
    }, 
    activated: { 
     type: Sequelize.BOOLEAN 
    } 
    }) 
User.belongsToMany(User, {as: "Follower", foreignKey: "FollowerId", through: "Follower_Followeds"}) 
User.belongsToMany(User, {as: "Followed", foreignKey: "FollowedId", through: "Follower_Followeds"}) 

,並設置關係:

app.post("/followhandler", function(req, res) { 
    let userId = req.session.userId 
    let followId = req.body.followId 

    User.findById(userId) 
    .then(currentUser => { 
     User.findById(followId) 
     .then(follows => { 
      currentUser.addUser(follows) 
     }) 
    }) 
    .catch(e => console.log(e)) 
}) 

但Follower_Followeds表根本不會得到更新。 我還試圖通過創建一個單獨的表和手動添加關係跳過多對多實施Sequelize的:

const FollowerFollowed = sequelize.define('followerFollowed', { 
    followedId: { 
    type: Sequelize.INTEGER 
    }, 

    followerId: { 
    type: Sequelize.INTEGER 
    } 
}) 

//define Users 
    const User = sequelize.define('user', { 
    firstName: { 
     type: Sequelize.STRING 
    }, 
    lastName: { 
     type: Sequelize.STRING 
    }, 
    email: { 
     type: Sequelize.STRING 
    }, 
    password: { 
     type: Sequelize.STRING 
    }, 
    activated: { 
     type: Sequelize.BOOLEAN 
    } 
    }) 

而且處理程序:

app.post("/followhandler", function(req, res) { 
    let userId = req.session.userId 
    let followId = req.body.followId 

    FollowerFollowed.create({ 
     followerId: userId, 
     followedId: followId 
    }) 
    .then(rel => console.log(rel)) 
    .catch(e => console.log(e)) 
}) 

但我得到在這種情況下,以下錯誤:

TypeError: val.replace is not a function at Object.SqlString.escape (/home/piepongwong/Documents/NYCDA/blog/node_modules/sequelize/lib/sql-string.js:61:15) at Object.escape (/home/piepongwong/Documents/NYCDA/blog/node_modules/sequelize/lib/dialects/abstract/query-generator.js:978:22) at Object.insertQuery (/home/piepongwong/Documents/NYCDA/blog/node_modules/sequelize/lib/dialects/abstract/query-generator.js:299:28) at QueryInterface.insert (/home/piepongwong/Documents/NYCDA/blog/node_modules/sequelize/lib/query-interface.js:497:33) at . (/home/piepongwong/Documents/NYCDA/blog/node_modules/sequelize/lib/instance.js:679:56) at tryCatcher (/home/piepongwong/Documents/NYCDA/blog/node_modules/bluebird/js/release/util.js:16:23) at Promise._settlePromiseFromHandler (/home/piepongwong/Documents/NYCDA/blog/node_modules/bluebird/js/release/promise.js:510:31) at Promise._settlePromise (/home/piepongwong/Documents/NYCDA/blog/node_modules/bluebird/js/release/promise.js:567:18) at Promise._settlePromise0 (/home/piepongwong/Documents/NYCDA/blog/node_modules/bluebird/js/release/promise.js:612:10) at Promise._settlePromises (/home/piepongwong/Documents/NYCDA/blog/node_modules/bluebird/js/release/promise.js:691:18) at Async._drainQueue (/home/piepongwong/Documents/NYCDA/blog/node_modules/bluebird/js/release/async.js:133:16) at Async._drainQueues (/home/piepongwong/Documents/NYCDA/blog/node_modules/bluebird/js/release/async.js:143:10) at Immediate.Async.drainQueues (/home/piepongwong/Documents/NYCDA/blog/node_modules/bluebird/js/release/async.js:17:14) at runCallback (timers.js:649:20) at tryOnImmediate (timers.js:622:5) at processImmediate [as _immediateCallback] (timers.js:594:5) TypeError: val.replace is not a function at Object.SqlString.escape (/home/piepongwong/Documents/NYCDA/blog/node_modules/sequelize/lib/sql-string.js:61:15) at Object.escape (/home/piepongwong/Documents/NYCDA/blog/node_modules/sequelize/lib/dialects/abstract/query-generator.js:978:22) at Object.insertQuery (/home/piepongwong/Documents/NYCDA/blog/node_modules/sequelize/lib/dialects/abstract/query-generator.js:299:28) at QueryInterface.insert (/home/piepongwong/Documents/NYCDA/blog/node_modules/sequelize/lib/query-interface.js:497:33) at . (/home/piepongwong/Documents/NYCDA/blog/node_modules/sequelize/lib/instance.js:679:56) at tryCatcher (/home/piepongwong/Documents/NYCDA/blog/node_modules/bluebird/js/release/util.js:16:23) at Promise._settlePromiseFromHandler (/home/piepongwong/Documents/NYCDA/blog/node_modules/bluebird/js/release/promise.js:510:31) at Promise._settlePromise (/home/piepongwong/Documents/NYCDA/blog/node_modules/bluebird/js/release/promise.js:567:18) at Promise._settlePromise0 (/home/piepongwong/Documents/NYCDA/blog/node_modules/bluebird/js/release/promise.js:612:10) at Promise._settlePromises (/home/piepongwong/Documents/NYCDA/blog/node_modules/bluebird/js/release/promise.js:691:18) at Async._drainQueue (/home/piepongwong/Documents/NYCDA/blog/node_modules/bluebird/js/release/async.js:133:16) at Async._drainQueues (/home/piepongwong/Documents/NYCDA/blog/node_modules/bluebird/js/release/async.js:143:10) at Immediate.Async.drainQueues (/home/piepongwong/Documents/NYCDA/blog/node_modules/bluebird/js/release/async.js:17:14) at runCallback (timers.js:649:20) at tryOnImmediate (timers.js:622:5) at processImmediate [as _immediateCallback] (timers.js:594:5)

回答

1

當您創建將M:M關聯中,你使用as: 'Follower'as: 'Followed'屬性秒。它會影響您添加/設置/刪除用戶之間關聯的方式。將隨後的用戶添加到其他用戶的方法現在稱爲addFollowed,並且將以與您所做的完全相同的方式使用。那麼你可以使用addFollower方法 -

User.findById(userId).then(currentUser => { 
    User.findById(followId).then(follows => { 
     currentUser.addFollowed(follows); // notice the difference here 
    }); 
}); 

同樣的,如果你想跟隨添加到特定用戶出現。

此外,在第二種解決方案中,您在創建關聯模型時犯了一個錯誤。在User模型中,您已經通過外鍵FollowerIdFollowedId定義了關聯,但是在FollowerFolloweds定義中,您創建了followerIdfollowedId,因此這兩者不匹配 - 您需要保持名稱一致。

編輯

根據在belongsToMany關係的文件中,as屬性應該被命名爲複數,所以你的情況,你應該命名這些as: 'Followers'as: 'Followeds'(第二個似乎有點怪)。 Sequelize本身單數化這些值,但是您可以使用對象自己定義它as: { singular: 'Follower', plural: 'Followers' }

The alias of this association. If you provide a string, it should be plural, and will be singularized using node.inflection. If you want to control the singular version yourself, provide an object with plural and singular keys.