【问题标题】:Client-generated JSON to be stored in MongoDB serverside, application/json not parsing correctly?客户端生成的 JSON 存储在 MongoDB 服务器端,应用程序/json 解析不正确?
【发布时间】:2015-01-05 12:57:29
【问题描述】:

我会很简短;我有一个client-side form,它允许创建“测验”,即一系列附有答案的问题,发送到服务器以存储到 MongoDB。我已尝试按照W3's specs 执行此操作,但承诺的结果与我得到的结果不符。

我希望我的 JSON 包含嵌套对象,如下所示:

{
    "_id": "54a884c68999af900fc28dcb",
    "name": "testquiz",
    "author": "user",
    "questions": [
        {
            "text": "question one",
            "answers": [
                {
                    "text": "answer one",
                    "correct": false
                },
                {
                    "text": "answer two",
                    "correct": true
                }
            ]
        },
        {
            "text": "question two",
            "answers": [
                {
                    "text": "answer one",
                    "correct": true
                },
                {
                    "text": "answer two",
                    "correct": false
                }
            ]
        }
    ]
}

但是,我只能得到这样的结果:

 {
    "_id": "54a8b00108039068102f8835",
    "quizname": "World War II",
    "questions[0][text]": "When did WWII start?",
    "questions[0][answers][0][text]": "1938",
    "questions[0][answers][1][iscorrect]": "on",
    "questions[0][answers][1][text]": "1939",
    "questions[0][answers][2][text]": "1944",
    "questions[0][answers][3][text]": "1914",
    "questions[1][text]": "",
    "questions[1][answers][0][text]": "",
    "questions[1][answers][1][text]": "",
    "author": "user"
}

我做错了什么?我是 MEAN 堆栈的新手,不胜感激。

【问题讨论】:

    标签: json node.js mongodb


    【解决方案1】:

    我想通了。我花费了令人尴尬的时间和痛苦的搜索量,但它确实有效。

        $('#savequiz').click(function () {
            obj = {
                quizname: $('#quizname').val(),
                author: "",
                questions: []
            };
    
            for (var i = 0; i < questionCount; i++) {
                obj.questions.push({
                    text: $('#question_' + i).val(),
                    answers: []
                });
    
                for (var j = 0; j < answerCount[i]; j++) {
                    obj.questions[i].answers.push({
                        text: $('#question_' + i + '_answer_' + j + ' > div > input').val(),
                        correct: $('#question_' + i + '_answer_' + j + ' > div > span > input[type="checkbox"]').is(':checked')
                    });
                }
            }
    
            $.ajax({
                url: '/quiz/new',
                type: "POST",
                data: JSON.stringify(obj),
                processData: false,
                contentType: "application/json; charset=UTF-8"
            });
    
            return false;
        });
    

    我相信解决方案体现在 $.ajax 调用参数'processData: false' 这仍然不能回答我的好奇心,为什么 W3 的表单提交规范与application/json 不符合规范,但至少我让它工作,并发布这个,希望如果其他人遇到它会有所帮助同样的问题。好久没有遇到这么看似简单的事情,但事实证明如此耗时。

    如果它有任何相关性或兴趣,这就是我处理请求的方式:

    app.post('/quiz/new', function (req, res, next) {
        if (!req.session.loggedin) {
          res.status(401).end();
          return;
        }
    
        var quiz = req.body;
        quiz.author = req.session.username;
    
        db.collection('quiz', function (err, collection) {
          collection.insert(quiz, {safe: true}, function (err, result) {
            if (err) {
                console.log('Failed to persist quiz!');
                res.send({'error': 'An error has occurred'});
                return;
            }
    
            console.log("Successfully persisted quiz.");
            res.redirect('/');
        });
    });
    

    【讨论】:

      猜你喜欢
      • 2013-05-01
      • 2018-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多