如何创建nodejs无模式结构?

时间:2017-12-21 06:41:38

标签: node.js mongodb

我正在开发nodejs后端,我想创建模式较少的模型结构。 我按照以下方式创建它。

questionnaireModel.js

var mongoose = require('mongoose');
var schema = mongoose.schema;
var bcrypt  = require ('bcrypt-nodejs');
var QuestionnaireSchema = mongoose.Schema({},{strict:false});

module.exports = mongoose.model('questionnaire', QuestionnaireSchema);

是不是?

我的控制器在这里:

questionnaireController.js

var mongoose = require('mongoose');
var config = require('../config');
var async = require('async');
var moment = require('moment');
var questionnaire = mongoose.model("questionnaire");

exports.createQuestionnaire = function (req, res) {
var receivedValues = req.body;
    var questionnaireData = new questionnaire();
    questionnaireData.categoryid = req.body.categoryid;
    questionnaireData.questiontype = req.body.questiontype;
    questionnaireData.isrequired = req.body.isrequired;
    questionnaireData.isshowvalue = req.body.isshowvalue;
    questionnaireData.options = req.body.options;
    questionnaireData.createdDate = moment().format();
    questionnaireData.save(function(err,resp){
            res.json({
              "code": 200,
              "status": "success",
            });
    });
}

但数据未插入集合中。

我使用Postman传递了以下数据:

{
 "categoryid":"123",
 "questiontype":"checkbox",
 "isrequired":true,
 "isshowvalue":true,
 "options":{
    "op1":"123",
    "op2":"1234"
 }
}

你能告诉我这里有什么问题吗?

提前致谢。

2 个答案:

答案 0 :(得分:0)

我已尝试过以下代码及其工作原理。

问卷架构

var mongoose = require('mongoose');
var Schema       = mongoose.Schema;

// var bcrypt  = require ('bcrypt-nodejs');


var QuestionnaireSchema = new Schema({

}, { strict: false });

module.exports = mongoose.model('questionnaire', QuestionnaireSchema);

路线:

var express = require('express');
var router = express.Router();

var mongoose = require('mongoose');
var config = require('../config/Database');


mongoose.connect(config.mongo_local,function(err){
    if(err){
        console.log("$$Database is not connected");
    }else{
        console.log("##Database is connected");
    }
});

// var async = require('async');
var moment = require('moment');
var questionnaire = require("../models/questionnaire");

router.post('/createQuestionnaire', function(req, res){
    console.log(req.body);
    var obj= {};
    obj = req.body;

    new questionnaire(obj).save(function(err,resp){
        if(err){
            console.log(err);
            res.json(err);
        }else{
            console.log("Data is created");
            res.json({
              "code": 200,
              "status": "success",
            });
        }
    })

});

当您执行{strict:false}时,需要在构造函数级别将值传递给模型,或者如果要创建对象然后分配值,则必须使用 set()

var questionnaireData = new questionnaire();
questionnaireData.set('categoryid', req.body.categoryid);

希望它会对你有所帮助。

答案 1 :(得分:0)

终于可行了。

questionnaireModel.js

var mongoose = require('mongoose'),
Schema = mongoose.Schema;

var QuestionnaireSchema = mongoose.Schema({ any: {}, },
{
  strict: false
});

module.exports = mongoose.model('questionnaire', QuestionnaireSchema, 'questionnaire');

questionnaireController.js

var mongoose = require('mongoose');
var config = require('../config');
var jwt = require('jsonwebtoken');
var async = require('async');
var moment = require('moment');

var questionnaire = mongoose.model("questionnaire");

exports.createQuestionnaire = function (req, res) {
    var requestBody= {};
    requestBody = req.body;

    questionnaire.create(requestBody,function(err,res){
          console.log("data instert successfully")
    })
 }

我使用create方法。