我要保存使用下面的架構在數據庫中的數據:創建路線
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"
}
]
我想寫路線(控制器)在nodejs使用rest api,但我不知道如何使用貓鼬在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 – abhaygarg12493
它不工作abhay,有人告訴我你必須推 –
推是在mongoDB查詢,但在路由,你可以簡單地發送數據列表 – abhaygarg12493