【问题标题】:Creating route for given schema according to json document根据 json 文档为给定模式创建路由
【发布时间】:2015-12-09 16:47:13
【问题描述】:

我想使用以下架构将数据保存在数据库中:

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

// Task schema
var taskSchema = mongoose.Schema({

 tasktype  : {type: String},
 createdon : {type: Date, default: Date.now},
 createdby : {type: Schema.Types.ObjectId,ref: 'User'},
 visitedby : [{type: Schema.Types.ObjectId,ref: 'User'}],
 taskinfo  : [{ isactive:Boolean, taskobject:String, taskdetails:String, iscompleted:Boolean}]  

});
module.exports = mongoose.model('Task', taskSchema);

这里是json文档的链接:Link

[
  {
    "_id": "55f4e4f5fe8b36980a611519",
    "tasktype": "Basic",
    "__v": 0,
    "taskinfo": [
      {
        "isactive":"True" ,
        "taskobject": "paid",
        "taskdetails": "This is task one",
        "iscompleted": "False"
      },
      {
        "isactive": "False",
        "taskobject": "free",
        "taskdetails": "This is task two",
        "iscompleted": "False"
      },
      {
        "isactive": "True",
        "taskobject": "paid",
        "taskdetails": "This is task three",
        "iscompleted": "False"
      },
      {
        "isactive": "True",
        "taskobject": "free",
        "taskdetails": "This is task four",
        "iscompleted": "False"
      }
    ],
    "visitedby": [],
    "createdon": "2015-09-13T02:52:37.512Z"
  }
]

我想使用rest api在nodejs中编写路由(控制器),但我不知道如何使用mongoose在mongodb中保存数组字段。

如果你能推荐我很好的资源,因为我买了一些 udemy 课程,但导师只告诉了 fieldname: res.body.fieldname 来接受用户输入 在这种情况下,这对我不起作用。

如果你能告诉我如何在我的数据库中保存数组,那就太好了,

这是不正确的示例路由文件,请帮助我

var Task     = require ('../models/task');
var User       = require ('../models/user');
var config     = require ('../../config');
module.exports = function(app, express) {

    var api = express.Router();

  api.post('/tasks', function (req, res) {
    var task = new Task({

          tasktype  : req.body.tasktype,
          taskinfo  : req.body.taskinfo,
      }); 

     task.save(function(err){
        if(err){
           res.send(err);
        return;
        }
       res.json({message:'Task has been created'})
      });
return api
}

Package.json [on request]
{
  "name": "todotask",
  "version": "1.0.0",
  "description": "test module",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "madhur",
  "license": "ISC",
  "dependencies": {
    "body-parser": "*",
    "express": "^4.13.3",
    "express-session": "*",
    "gulp": "^3.9.0",
    "gulp-nodemon": "^2.0.3",
    "mongoose": "*",
    "morgan": "*",
    "multer": "^1.0.3",
    "passport": "*",
    "passport-facebook": "*",
    "passport-google": "*",
    "passport-http": "*",
    "passport-local": "*",
    "passport-twitter": "*",
    "serve-favicon": "*",
    "socket.io": "*"
  }
}

谢谢

【问题讨论】:

  • 可以在req.body中发送数组
  • 它不起作用,有人告诉我你必须推动
  • push 在 mongoDB 查询中,但是在路由中你可以简单地在列表中发送数据
  • 你检查过我创建的路线了吗?你能告诉我其中的编辑吗?
  • 你要在Task Document的哪些字段中保存Array,我给你写代码

标签: json node.js mongodb mongoose


【解决方案1】:

从客户端发送此类数据

taskinfo:[{
        "isactive":"True" ,
        "taskobject": "paid",
        "taskdetails": "This is task one",
        "iscompleted": "False"
      },{
        "isactive": "False",
        "taskobject": "free",
        "taskdetails": "This is task two",
        "iscompleted": "False"
      }]

从客户端站点使用邮递员(谷歌应用程序)发送此数据。如果有任何错误,请粘贴该错误

【讨论】:

  • TypeError: 对象不是函数
       在 C:\projects\rt\app\routes\api.js:16:26
       在 Layer.handle [as handle_request] (C:\projects\rt\node_modules\express\lib\router\layer.js:95:5) 无法发布完整错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-19
  • 2019-06-19
相关资源
最近更新 更多