POST节点j不能形成

时间:2018-01-19 17:03:06

标签: html node.js express

编辑2

所以我试过了

router.get('/dashboard/users/forms/competition-form/:id', ensureAuthenticated, (req, res, next) => {
  CompetitionForm.find(req.params.id, function(err, CompetitionForm){
    res.render('dashboard/users/forms/competition-form.hbs', {
      pageTitle: 'Competition Form',
      user_id: req.params.id
    });
  });
});

如@YouneL所述,我也编辑了以下

的帖子路线
// competition form edit
router.post('/dashboard/users/forms/competition-form/:id', (req, res) => {
  var schoolName = req.body.schoolName;
  var competitionDate = req.body.competitionDate;
  var competitionTime = req.body.competitionTime;
  var competitionVenue = req.body.competitionVenue;
  var competitionTotalOfStudents = req.body.competitionTotalOfStudents;
  var competitionTotalParticipated = req.body.competitionTotalParticipated;
  var competitionTotalPersonnel = req.body.competitionTotalPersonnel;
  var competitionJudge1Name = req.body.competitionJudge1Name;
  var competitionJudge1Telephone = req.body.competitionJudge1Telephone;
  var competitionJudge1Email = req.body.competitionJudge1Email;
  var competitionJudge2Name = req.body.competitionJudge2Name;
  var competitionJudge2Telephone = req.body.competitionJudge2Telephone;
  var competitionJudge2Email = req.body.competitionJudge2Email;
  var competitionJudge3Name = req.body.competitionJudge3Name;
  var competitionJudge3Telephone = req.body.competitionJudge3Telephone;
  var competitionJudge3Email = req.body.competitionJudge3Email;

  // admin fields
  var competitionRequiredPhotos = req.body.competitionRequiredPhotos;
  var competitionRequiredCertifications = req.body.competitionRequiredCertification;

  console.log(CompetitionForm);
  console.log('form successful');
  res.redirect('/dashboard');
});

所以现在当提交表单时,我会被重定向到仪表板,但是没有对数据库的更新,应该有一个名为CompetitionForm的竞赛表单的新集合

enter image description here

编辑1

我在邮政路线上运行console.log(CompetitionForm);,我得到以下输出。

[nodemon] restarting due to changes...
[nodemon] starting `node app.js hbs, css`
Server is up on port 3000
Connection has been established
Connection has been established
Fri Jan 19 2018 17:15:21 GMT+0000 (GMT): GET /dashboard/users/forms/competition-form/5a5c6740b9e210087e098fd6
Fri Jan 19 2018 17:15:21 GMT+0000 (GMT): GET /users/login
Fri Jan 19 2018 17:15:22 GMT+0000 (GMT): GET /favicon.ico
Fri Jan 19 2018 17:15:29 GMT+0000 (GMT): POST /users/login
Fri Jan 19 2018 17:15:29 GMT+0000 (GMT): GET /users/login
Fri Jan 19 2018 17:15:29 GMT+0000 (GMT): GET /favicon.ico
Fri Jan 19 2018 17:15:34 GMT+0000 (GMT): POST /users/login
Fri Jan 19 2018 17:15:34 GMT+0000 (GMT): GET /dashboard
Fri Jan 19 2018 17:15:34 GMT+0000 (GMT): GET /favicon.ico
Fri Jan 19 2018 17:15:36 GMT+0000 (GMT): GET /dashboard/users/forms/competition-form/5a5c6740b9e210087e098fd6
Fri Jan 19 2018 17:15:37 GMT+0000 (GMT): GET /favicon.ico
Fri Jan 19 2018 17:16:07 GMT+0000 (GMT): POST /dashboard/users/forms/competition-form/5a5c6740b9e210087e098fd6
{ [Function: model]
  hooks: Kareem { _pres: {}, _posts: {} },
  base: 
   Mongoose {
     connections: [ [NativeConnection] ],
     models: { User: [Function], CompetitionForm: [Circular] },
     modelSchemas: { User: [Schema], CompetitionForm: [Schema] },
     options: { pluralization: true },
     plugins: [ [Array], [Array], [Array] ] },
  modelName: 'CompetitionForm',
  model: [Function: model],
  db: 
   NativeConnection {
     base: 
      Mongoose {
        connections: [Array],
        models: [Object],
        modelSchemas: [Object],
        options: [Object],
        plugins: [Array] },
     collections: 
      { users: [NativeCollection],
        competitionforms: [NativeCollection] },
     models: { User: [Function], CompetitionForm: [Circular] },
     config: { autoIndex: true },
     replica: false,
     hosts: null,
     host: 'localhost',
     port: 27017,
     user: null,
     pass: null,
     name: 'pol-development',
     options: null,
     otherDbs: [],
     states: 
      { '0': 'disconnected',
        '1': 'connected',
        '2': 'connecting',
        '3': 'disconnecting',
        '4': 'unauthorized',
        '99': 'uninitialized',
        disconnected: 0,
        connected: 1,
        connecting: 2,
        disconnecting: 3,
        unauthorized: 4,
        uninitialized: 99 },
     _readyState: 1,
     _closeCalled: false,
     _hasOpened: true,
     _listening: false,
     _connectionOptions: {},
     _events: { error: [Array] },
     _eventsCount: 1,
     db: 
      Db {
        domain: null,
        _events: [Object],
        _eventsCount: 2,
        _maxListeners: undefined,
        s: [Object],
        serverConfig: [Getter],
        bufferMaxEntries: [Getter],
        databaseName: [Getter] } },
  discriminators: undefined,
  '$appliedMethods': true,
  '$appliedHooks': true,
  _events: { init: [Function], save: [Function] },
  _eventsCount: 2,
  schema: 
   Schema {
     obj: 
      { schoolName: [Function: String],
        competitionDate: [Function: String],
        competitionTime: [Function: String],
        competitionVenue: [Function: String],
        competitionTotalOfStudents: [Function: Number],
        competitionTotalParticipated: [Function: Number],
        competitionTotalPersonnel: [Function: Number],
        competitionJudge1Name: [Function: String],
        competitionJudge1Telephone: [Function: String],
        competitionJudge1Email: [Function: String],
        competitionJudge2Name: [Function: String],
        competitionJudge2Telephone: [Function: String],
        competitionJudge2Email: [Function: String],
        competitionJudge3Name: [Function: String],
        competitionJudge3Telephone: [Function: String],
        competitionJudge3Email: [Function: String],
        competitionRequiredPhotos: [Function: Boolean],
        competitionRequiredCertifications: [Function: Boolean] },
     paths: 
      { schoolName: [SchemaString],
        competitionDate: [SchemaString],
        competitionTime: [SchemaString],
        competitionVenue: [SchemaString],
        competitionTotalOfStudents: [SchemaNumber],
        competitionTotalParticipated: [SchemaNumber],
        competitionTotalPersonnel: [SchemaNumber],
        competitionJudge1Name: [SchemaString],
        competitionJudge1Telephone: [SchemaString],
        competitionJudge1Email: [SchemaString],
        competitionJudge2Name: [SchemaString],
        competitionJudge2Telephone: [SchemaString],
        competitionJudge2Email: [SchemaString],
        competitionJudge3Name: [SchemaString],
        competitionJudge3Telephone: [SchemaString],
        competitionJudge3Email: [SchemaString],
        competitionRequiredPhotos: [SchemaBoolean],
        competitionRequiredCertifications: [SchemaBoolean],
        _id: [ObjectId],
        __v: [SchemaNumber] },
     aliases: {},
     subpaths: {},
     virtuals: { id: [VirtualType] },
     singleNestedPaths: {},
     nested: {},
     inherits: {},
     callQueue: [ [Array], [Array], [Array], [Array], [Array], [Array] ],
     _indexes: [],
     methods: {},
     statics: {},
     tree: 
      { schoolName: [Function: String],
        competitionDate: [Function: String],
        competitionTime: [Function: String],
        competitionVenue: [Function: String],
        competitionTotalOfStudents: [Function: Number],
        competitionTotalParticipated: [Function: Number],
        competitionTotalPersonnel: [Function: Number],
        competitionJudge1Name: [Function: String],
        competitionJudge1Telephone: [Function: String],
        competitionJudge1Email: [Function: String],
        competitionJudge2Name: [Function: String],
        competitionJudge2Telephone: [Function: String],
        competitionJudge2Email: [Function: String],
        competitionJudge3Name: [Function: String],
        competitionJudge3Telephone: [Function: String],
        competitionJudge3Email: [Function: String],
        competitionRequiredPhotos: [Function: Boolean],
        competitionRequiredCertifications: [Function: Boolean],
        _id: [Object],
        __v: [Function: Number],
        id: [VirtualType] },
     query: {},
     childSchemas: [],
     plugins: [ [Object], [Object], [Object], [Object] ],
     s: { hooks: [Kareem], kareemHooks: [Object] },
     _userProvidedOptions: undefined,
     options: 
      { retainKeyOrder: false,
        typeKey: 'type',
        id: true,
        noVirtualId: false,
        _id: true,
        noId: false,
        validateBeforeSave: true,
        read: null,
        shardKey: null,
        autoIndex: null,
        minimize: true,
        discriminatorKey: '__t',
        versionKey: '__v',
        capped: false,
        bufferCommands: true,
        strict: true,
        pluralization: true },
     '$globalPluginsApplied': true },
  collection: 
   NativeCollection {
     collection: Collection { s: [Object] },
     opts: 
      { bufferCommands: true,
        capped: false,
        '$wasForceClosed': undefined },
     name: 'competitionforms',
     collectionName: 'competitionforms',
     conn: 
      NativeConnection {
        base: [Mongoose],
        collections: [Object],
        models: [Object],
        config: [Object],
        replica: false,
        hosts: null,
        host: 'localhost',
        port: 27017,
        user: null,
        pass: null,
        name: 'pol-development',
        options: null,
        otherDbs: [],
        states: [Object],
        _readyState: 1,
        _closeCalled: false,
        _hasOpened: true,
        _listening: false,
        _connectionOptions: {},
        _events: [Object],
        _eventsCount: 1,
        db: [Db] },
     queue: [],
     buffer: false,
     emitter: 
      EventEmitter {
        domain: null,
        _events: {},
        _eventsCount: 0,
        _maxListeners: undefined } },
  Query: 
   { [Function]
     base: 
      Query {
        toConstructor: [Function: toConstructor],
        setOptions: [Function],
        collection: [Function: collection],
        '$where': [Function],
        where: [Function],
        equals: [Function: equals],
        eq: [Function: eq],
        or: [Function: or],
        nor: [Function: nor],
        and: [Function: and],
        gt: [Function],
        gte: [Function],
        lt: [Function],
        lte: [Function],
        ne: [Function],
        in: [Function],
        nin: [Function],
        all: [Function],
        regex: [Function],
        size: [Function],
        maxDistance: [Function],
        minDistance: [Function],
        mod: [Function],
        exists: [Function],
        elemMatch: [Function],
        within: [Function: within],
        box: [Function],
        polygon: [Function],
        circle: [Function],
        near: [Function: near],
        intersects: [Function: intersects],
        geometry: [Function: geometry],
        select: [Function: select],
        slice: [Function],
        sort: [Function],
        limit: [Function],
        skip: [Function],
        maxScan: [Function],
        batchSize: [Function],
        comment: [Function],
        maxTime: [Function],
        snapshot: [Function],
        hint: [Function],
        slaveOk: [Function],
        read: [Function],
        tailable: [Function],
        merge: [Function],
        find: [Function],
        cursor: [Function: cursor],
        findOne: [Function],
        count: [Function],
        distinct: [Function],
        update: [Function: update],
        updateMany: [Function: updateMany],
        updateOne: [Function: updateOne],
        replaceOne: [Function: replaceOne],
        remove: [Function],
        deleteOne: [Function],
        deleteMany: [Function],
        findOneAndUpdate: [Function],
        findOneAndRemove: [Function],
        _findAndModify: [Function],
        _wrapCallback: [Function],
        setTraceFunction: [Function],
        exec: [Function: exec],
        thunk: [Function],
        then: [Function],
        stream: [Function],
        selected: [Function: selected],
        selectedInclusively: [Function: selectedInclusively],
        selectedExclusively: [Function: selectedExclusively],
        _mergeUpdate: [Function],
        _optionsForExec: [Function],
        _fieldsForExec: [Function],
        _updateForExec: [Function],
        _ensurePath: [Function],
        _validate: [Function] } },
  '$__insertMany': [Function],
  insertMany: [Function],
  '$init': Promise { [Circular] } }

原始

我正在尝试根据用户ID在信息中心内创建一个nodejs表单,但由于某种原因它无法发布。

以下是一些代码:

<form action="/dashboard/users/forms/competition-form/{{user.id}}" method="post">

路由

// competition form details
router.get('/dashboard/users/forms/competition-form/:id', ensureAuthenticated, (req, res, next) => {
  CompetitionForm.find(req.params.id, function(err, CompetitionForm){
    res.render('dashboard/users/forms/competition-form.hbs', {
      pageTitle: 'Competition Form',
    });
  });
});

// competition form edit
router.post('/dashboard/users/forms/competition-form/:id', (req, res) => {
  var schoolName = req.body.schoolName;
  var competitionDate = req.body.competitionDate;
  var competitionTime = req.body.competitionTime;
  var competitionVenue = req.body.competitionVenue;
  var competitionTotalOfStudents = req.body.competitionTotalOfStudents;
  var competitionTotalParticipated = req.body.competitionTotalParticipated;
  var competitionTotalPersonnel = req.body.competitionTotalPersonnel;
  var competitionJudge1Name = req.body.competitionJudge1Name;
  var competitionJudge1Telephone = req.body.competitionJudge1Telephone;
  var competitionJudge1Email = req.body.competitionJudge1Email;
  var competitionJudge2Name = req.body.competitionJudge2Name;
  var competitionJudge2Telephone = req.body.competitionJudge2Telephone;
  var competitionJudge2Email = req.body.competitionJudge2Email;
  var competitionJudge3Name = req.body.competitionJudge3Name;
  var competitionJudge3Telephone = req.body.competitionJudge3Telephone;
  var competitionJudge3Email = req.body.competitionJudge3Email;

  // admin fields
  var competitionRequiredPhotos = req.body.competitionRequiredPhotos;
  var competitionRequiredCertifications = req.body.competitionRequiredCertification;
});

我期待的是

当用户登录时,他们应该能够填写此表单,用户属于此表单,因此它基于用户ID。

不太清楚我在这里缺少什么。

1 个答案:

答案 0 :(得分:1)

您应该更改视图以使用路由处理程序传递的user_id(即router.get(...)),同时使用Model.findOneAndUpdate()将发布的数据保存到数据库中:

查看:

<form action="/dashboard/users/forms/competition-form/{{ user_id }}" method="post">

邮政处理程序:

router.post('/dashboard/users/forms/competition-form/:id', (req, res) => {

    CompetitionForm.findOneAndUpdate( { _id: req.params.id }, req.body, (err, competition) => {

        if (err) {
            console.log(`Error saving data:  ${err}`);
            return res.send('Error saving data');
        }

        res.redirect('/dashboard');

    } );

});