Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用mongoose将javascript数组插入mongodb时出错_Javascript_Arrays_Node.js_Mongodb - Fatal编程技术网

使用mongoose将javascript数组插入mongodb时出错

使用mongoose将javascript数组插入mongodb时出错,javascript,arrays,node.js,mongodb,Javascript,Arrays,Node.js,Mongodb,我对node和mongodb还很陌生。我试图使用mongoose向mongodb插入一个javascript数组变量。但这会导致一个错误 运行此代码时,我收到一条错误消息: ValidationError: CastError: Cast to Array failed for value "[object Object],[object Object],[object Object]" at path "questions" 这是我定义的模式 var mongoose = require('

我对node和mongodb还很陌生。我试图使用mongoose向mongodb插入一个javascript数组变量。但这会导致一个错误

运行此代码时,我收到一条错误消息:

ValidationError: CastError: Cast to Array failed for value "[object Object],[object Object],[object Object]" at path "questions"
这是我定义的模式

var mongoose = require('mongoose');
var questionSchema = new mongoose.Schema({
    questionSet : String,
    // questionTime:Number,
    questions:[{
        //questionID : String,
        questionNo : String,
        questionSection : String,
        questionStatement : String,
        answerA : String,
        answerB : String,
        answerC : String,
        answerD : String,
        correctAnswer : String
    }]
});
module.exports = mongoose.model('Question', questionSchema);
为了使用mongoose将数据插入mongodb,我使用以下代码

var Question = require('../schemas/questions');
exports.testing = function(req,res){
    if (!req.body) return res.sendStatus(400)

    var ques_set = req.body.set;
    var question_array = req.body.ques;
var data = Question({question_set: ques_set, questions: question_array});

    data.save(function(err) {
        if (err) throw err;
        else {
            console.log('Question Inserted');
            res.send("Question Inserted");    
        }
    });
  };
我正在使用这个javascript创建一个类似于我的模式的问题数组。这里我使用
push()
方法创建一个问题数组

function myFunction1(){
        document.getElementById("questionSet").disabled = true;

        var questionSet = document.getElementById("form_manu").elements[0].value;
    }

function myFunction3(){
    if(count < totalQuestion) {
        question.push({
            questionID:document.getElementById("form").elements[4].value,
            questionSection:document.getElementById("form").elements[5].value,
            questionStatement:document.getElementById("form").elements[6].value,
            answerA : document.getElementById("form").elements[7].value,
            answerB : document.getElementById("form").elements[8].value,
            answerC : document.getElementById("form").elements[9].value,
            answerD : document.getElementById("form").elements[10].value,
            correctAnswer : document.getElementById("form").elements[11].value
        });
我使用
Send

我已经在控制台上打印了变量
ques\u set
question\u array
ques\u set
打印出我传入的字符串,但是
question\u数组
只显示
[object object],[object object]

更新2
当我在变量问题上使用JSON.stringify()时,其显示如下

[{"questionNo":"1","questionSection":"sec1","questionStatement":"ques1","answerA":"string1","answerB":"string2","answerC":"string3","answerD":"string4","correctAnswer":"A"},{"questionNo":"2","questionSection":"sec2","questionStatement":"Ques2","answerA":"string1","answerB":"string2","answerC":"string3","answerD":"string4","correctAnswer":"B"}]

我知道这个描述很长,但我不能减少它。我道歉

我不确定您的JSON.stringify(question_array)示例是在浏览器上还是在服务器上运行。所以我要在这里冒险

您的发布地址为:

<button onclick="post('question', {set: questionSet, ques : question })">Send</button>
更新

事实上,表单生成代码正在执行以下操作:

hiddenField.setAttribute("value", params[key]);
因此,在某个时刻,
params[“ques”]
包含一个对象数组,而不是字符串。JS将为
问题
数组中包含的每个
{}
对象打印
[object object]
。您可以这样做:

hiddenField.setAttribute("value", JSON.stringify(params[key]));

将questionSchema变量定义更改为:

var questionVariable = new mongoose.Schema({
    //questionID : String,
    questionNo : String,
    questionSection : String,
    questionStatement : String,
    answerA : String,
    answerB : String,
    answerC : String,
    answerD : String,
    correctAnswer : String
});

var questionSchema = new mongoose.Schema({
    questionSet : String,
    // questionTime:Number,
    questions:[questionVariable]
});

这是必需的,因为mongoose无法在没有相关模式的情况下解析对象。现在,当您为内部问题对象创建一个新模式并在主问题中引用它时,mongoose应该能够解析您的对象

您可以在创建对象并将其发送到服务器的位置添加代码吗?i、 e.您在myFunction3中创建的问题变量是如何添加到对象并发送的?很可能您的
req.body.ques
不是您所认为的(数组)。将客户端窗体显示为well@MarkHughes,laggingreflection:谢谢你花时间。我已经更新了如何发送变量。请检查是否可以使用JSON.stringify(question_array)将其显示为字符串?@cburatto感谢您检查我的问题。我已经更新了。请检查一下。
hiddenField.setAttribute("value", JSON.stringify(params[key]));
var questionVariable = new mongoose.Schema({
    //questionID : String,
    questionNo : String,
    questionSection : String,
    questionStatement : String,
    answerA : String,
    answerB : String,
    answerC : String,
    answerD : String,
    correctAnswer : String
});

var questionSchema = new mongoose.Schema({
    questionSet : String,
    // questionTime:Number,
    questions:[questionVariable]
});