[英]Error while inserting javascript array to mongodb using mongoose
我對node和mongodb很陌生。 我試圖使用mongoose將javascript數組變量插入mongodb。 但這會導致錯誤。
運行此代碼時,我收到一條錯誤消息:
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
});
更新1
要發送javascript變量,請使用以下javascript函數
function post(path, params, method) {
method = method || "post"; // Set method to post by default if not specified.
// The rest of this code assumes you are not using a library.
// It can be made less wordy if you use one.
var form = document.createElement("form");
form.setAttribute("method", method);
form.setAttribute("action", path);
for(var key in params) {
if(params.hasOwnProperty(key)) {
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", key);
hiddenField.setAttribute("value", params[key]);
form.appendChild(hiddenField);
}
}
document.body.appendChild(form);
form.submit();
}
我使用<button onclick="post('question', {set: questionSet, ques : question })">Send</button>
從html主體中調用此javascript函數。
我已經在控制台上打印了變量ques_set
和question_array
。 ques_set
打印出我要傳入的字符串,但是question_array
僅顯示[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>
我的猜測是,因為questionSet
確實是一個字符串,所以在set
上沒有出現錯誤-但是在傳輸之前/期間question
並未被字符串化(服務器實際上接收到一個在生成的字符串[object Object],[object Object]
瀏覽器,或者客戶端或服務器上存在某種dataType問題。
您可以先嘗試以下方法:
<button onclick="post('question', {set: questionSet, ques : JSON.stringify(question) })">Send</button>
更新 :
實際上,您的表單生成代碼正在執行此操作:
hiddenField.setAttribute("value", params[key]);
因此, params["ques"]
有時包含對象數組,而不是字符串。 JS將為question
數組中包含的每個{}
對象打印[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]
});
這是必需的,因為貓鼬在沒有相關模式的情況下無法解析對象。 現在,當您為內部問題對象創建新的架構並在主questionSchema中引用它時,貓鼬應該能夠解析您的對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.