无法在Node.js中使用mongoose将JSON插入mongoDB

时间:2015-07-02 22:24:20

标签: json node.js mongodb

我无法在node.js中使用mongoose更新我的mongoDB

这是我的架构:

var bookSchema = mongoose.Schema({
_creator: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
content: JSON,
views: Number,
name: String,
genre: String,
Description: String,
dateOfCreation: Date,
published: Boolean
})

所以'content'是一个JSON元素。 在之前的查询中,我将'content'插入以下JSON:

{
    "author": "AuthorName",
    "series": "SeriesNameIfPartOfASeries",
    "name": "NameOfBook",
    "page": []
}

我的问题是我在编辑JSON后无法更新数据库。

我正在尝试将其添加到页面数组中:

{
"img": "urlOfImage",
"frame": [{
    "id": "0",
    "cx": "0",
    "cy": "0",
    "scale": "1",
    "rotation": "0",
    "filters": "",
    "transition":
        {
            "type": "move",
            "time": "0",
            "easing": "linear"
        },
    "requireUserInput": "true",
    "cropping":
        {
            "visible": "false",
            "shape": "rect",
            "color": "rgb(0,0,0)",
            "opacity": "1",
            "x": [],
            "y": []
        }
}]

}

由于某种原因,以下代码不会抛出任何错误,但不会更新数据库。 另外,如果我在b.save()函数中的console.log(b.content),它似乎已更新它,但我的数据库保持不变。

    function (req,res) {
    Book.findOne({_id: req.body.bookID, _creator: req.user._id },function (err, b) {
        if (err)
            throw(err)
        var page = {
            "img": req.files.file.name,
            "frame": [{
                "id": "0",
                "cx": "0",
                "cy": "0",
                "scale": "1",
                "rotation": "0",
                "filters": "",
                "transition":
                {
                    "type": "move",
                    "time": "0",
                    "easing": "linear"
                },
                "requireUserInput": "true",
                "cropping":
                {
                    "visible": "false",
                    "shape": "rect",
                    "color": "rgb(0,0,0)",
                    "opacity": "1",
                    "x": [],
                    "y": []
                }
            }]
        }
        b.content.page.push(page)
        b.save(function (err) {
            if(err)
                throw(err)
        //the following returns the book WITH the right data. What is going
        //on?
        console.log(b.content)
        })
    }

编辑1: --------------------------------------

尝试使用markModified()仍然无法正常工作。以下是我的更新代码以及建议的解决方案:

    function (req,res) {
        Book.findOne({_id:req.body.bookID, _author: req.user._id},function(err,b) {
            if(err)
                throw(err);
            if(b) {
                var page = {
                    "img": req.files.file.name,
                    "frame": [{
                        "id": "0",
                        "cx": "0",
                        "cy": "0",
                        "scale": "1",
                        "rotation": "0",
                        "filters": "",
                        "transition":
                        {
                            "type": "move",
                            "time": "0",
                            "easing": "linear"
                        },
                        "requireUserInput": "true",
                        "cropping":
                        {
                            "visible": "false",
                            "shape": "rect",
                            "color": "rgb(0,0,0)",
                            "opacity": "1",
                            "x": [],
                            "y": []
                        }
                    }]
                }
                b.content.page.push(page)
                b.markModified('content')
                b.save(function (err) {
                    if(err)
                        throw(err);
                })
            }
        })


    res.end('200')
}

1 个答案:

答案 0 :(得分:0)

当你使用save()方法时,有时候Mongoose并不知道某些东西已经改变了。您可以使用markModified()将数组标记为已修改:

UICollectionView