节点JS从Sqlite db更新对象

时间:2017-06-22 00:41:07

标签: node.js

我想在节点js中更新sqlite数据库中的一个值,但我无法编辑该值,我回来了。调试结果是user.friend未定义。

app.put('/api/acceptFriend', function (req, res, next) {
    var requestFrom = req.body.requestFrom;
    var requestTo = req.body.requestTo;
    var friends = req.body.friends;

    User.findAll({
        where: {username: requestTo}
    }).then(function (user) {
        user.friends = friends;
        user.save();

        res.status(200).json({message: "Friend successfully added."});
    }).catch(function (error) {
        res.status(500).json({message: "Something went wrong", reason: reason.original.message});
    });

1 个答案:

答案 0 :(得分:0)

解决方案是

app.put('/api/acceptFriend', function (req, res, next) {
    var requestFrom = req.body.requestFrom;
    var requestTo = req.body.requestTo;
    var friends = req.body.friends;

    User.find({
        where: {username: requestTo}
    }).then(function (user) {

        if(user){
            user.updateAttributes({
                friends: friends
            }).then(function () {
                res.status(200).json({message: "Friend successfully added."});
            })
        }

    }).catch(function (error) {
        res.status(500).json({message: "Something went wrong: " + error});
    });