简体   繁体   中英

How to add an array to a javascript server side model in MEAN.JS

I am using MEAN.JS and I have created a module for projects. I would like to add tasks to these projects and I would like to do it with a multi-dimensional array. I would like the array to include a task.description and a task.status which would both be strings. I think I understand the client-side part of my project and I know there are still other files. However, I believe this will make the question as simple as possible, as I am struggling to get my profile developed on this site. I will however include controller.js, so I can get this working and hopefully give credit for a correct answer.

project.server.model.js

 'use strict'; /** * Module dependencies. */ var mongoose = require('mongoose'), Schema = mongoose.Schema; /** * Project Schema */ var ProjectSchema = new Schema({ created: { type: Date, default: Date.now }, title: { type: String, default: '', trim: true, required: 'Title cannot be blank' }, description: { type: String, default: '', trim: true }, /* MODEL for TASK ARRAY*/ task: { }, user: { type: Schema.ObjectId, ref: 'User' } }); mongoose.model('Project', ProjectSchema); 

projects.server.controller.js

 'use strict'; /** * Module dependencies. */ var path = require('path'), mongoose = require('mongoose'), Project = mongoose.model('Project'), errorHandler = require(path.resolve('./modules/core/server/controllers/errors.server.controller')); /** * Create a project */ exports.create = function (req, res) { var project = new Project(req.body); project.user = req.user; project.save(function (err) { if (err) { return res.status(400).send({ message: errorHandler.getErrorMessage(err) }); } else { res.json(project); } }); }; /** * Show the current project */ exports.read = function (req, res) { res.json(req.project); }; /** * Update a project */ exports.update = function (req, res) { var project = req.project; project.title = req.body.title; project.description = req.body.description; project.save(function (err) { if (err) { return res.status(400).send({ message: errorHandler.getErrorMessage(err) }); } else { res.json(project); } }); }; 

edit-project.client.view.html

 <section ng-controller="ProjectsController" ng-init="findOne()"> <div class="page-header"> <h1>Edit Project</h1> </div> <div class="col-md-12"> <form name="projectForm" class="form-horizontal" ng-submit="update(projectForm.$valid)" novalidate> <fieldset> <div class="form-group" show-errors> <label for="title">Title</label> <input name="title" type="text" ng-model="project.title" id="title" class="form-control" placeholder="Title" required> <div ng-messages="projectForm.title.$error" role="alert"> <p class="help-block error-text" ng-message="required">Project title is required.</p> </div> </div> <div class="form-group"> <label for="description">Description</label> <textarea name="description" ng-model="project.description" id="description" class="form-control" cols="30" rows="4" placeholder="Description"></textarea> </div> <div class="form-group"> Task Description <textarea name="description" ng.model="project.task.description" class="form-control" cols="30" rows="3" placeholder="Description"></textarea> <div> Task Status <input name="status" ng.model="project.task.status" class="form-control" placeholder="Status"> </div> </div> <div class="form-group"> <input type="submit" value="Update" class="btn btn-default"> </div> <div ng-show="error" class="text-danger"> <strong ng-bind="error"></strong> </div> </fieldset> </form> </div> </section> 

First, create a model for tasks (task.server.model.js), which references a Project

var TaskSchema = new mongoose.Schema({
    description: String,
    status: String,
    // referencing Project model
    project: { type: mongoose.Schema.Types.ObjectId, ref: 'Project' }
});

And then in Project model reference Task // Add this to Project Schema definition

tasks: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Task' }]

It's should

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

/**
 * Task Schema 
 */
var TaskSchema = new Schema({
    description: String,
    status: String,
    project: {
        type: Schema.ObjectId,
        ref: 'Project'
    }
});

Hope it's help you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM