Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.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
Javascript 不可数未定义_Javascript_Html_Mongoose - Fatal编程技术网

Javascript 不可数未定义

Javascript 不可数未定义,javascript,html,mongoose,Javascript,Html,Mongoose,这是html文件,并不重要 <body> <!-- Select Radio Button --> <label for="radio1">Select</label> <input type="radio" name="radio1" id="radio1" > <!-- Multi-

这是html文件,并不重要

<body>
        <!-- Select Radio Button -->
        <label for="radio1">Select</label>
        <input type="radio" name="radio1" id="radio1"  >
        <!-- Multi-Select Radio Button -->
        <label for="radio2"  >Multi-Select</label>
        <input type="radio" name="radio1" id="radio2" >
        <br>
        <!-- Items -->
        <label for="radio2">Item 1</label>
        <input type="radio" name="item" id="item0" value="item1">
        <label for="radio2">Item 2</label>
        <input type="radio" name="item" id="item1"  value="item2">
        <label for="radio2">Item 3</label>
        <input type="radio" name="item" id="item2" value="item3">
        <label for="radio2">Item 4</label>
        <input type="radio" name="item" id="item3" value="item4">
        <button type="submit" class="btn " onclick="handling_dbs()">Submit</button>
</body>
<script src="./index.js"></script>

挑选
多选

项目1 项目2 项目3 项目4 提交
现在是index.js文件

var mongoose = require('mongoose')
mongoose.connect('mongodb://localhost:27017/pro',{useNewUrlParser:true}); 
var db = mongoose.connection;
db.on('error',console.error.bind(console,'connection error'))
var selectionSchema = new mongoose.Schema({
    selection_type : {type:String, required:true},
    selected : {type:String, required:true}
})

                                        

var selections =  mongoose.model('selection',selectionSchema);
console.log(selections)  //Added this to check what's inside the selections variable

function handling_dbs(){ 
     console.log(selections) //Added this to check what's inside the selections variable
    if(document.getElementById('radio1').checked){
        let length=4;
        for(let i=0;i<length;i++){
            if(document.getElementById(`item${i}`).checked){
                let selected_item = document.getElementById(`item${i}`).value;
            }
        }
        
        let doc = new selections({
            selection_type:'Select',
            selected:   `${selected_item}`
        });
        doc.save();
        console.log('Yes')
    }
}
var mongoose=require('mongoose'))
猫鼬mongodb://localhost:27017/pro“,{useNewUrlParser:true});
var db=猫鼬连接;
db.on('error',console.error.bind(console,'connection error'))
var selectionSchema=newmongoose.Schema({
选择类型:{type:String,必需:true},
所选:{type:String,必需:true}
})
var selections=mongoose.model('selection',selectionSchema);

console.log(selections)//添加此选项以检查selections变量中的内容 函数处理_dbs(){
console.log(selections)//添加此选项以检查selections变量中的内容 if(document.getElementById('radio1')。选中){ 设长度=4;
对于(设i=0;i我认为mongoose.connect是一个异步函数,因此当您将“mongoose.model('selection',selectionSchema);”的值赋给selections常量时,该值还不可用。 将selections常量的初始化移到handling_dbs()函数中应该可以解决这个问题。
一个更复杂的解决方案是等待db连接建立,然后才让click listener工作。

哪个变量未定义?在哪个函数中-大概在
处理_dbs
中?选择变量以及控制台显示了什么?记住,并不是每个人都使用过Mongoose-它可能是异步的吗它揭示了函数之外的内部内容