I have a single webpage that initially has two form inputs, one for a list of names and another for the title of a game. I've written some javascript/jquery that takes the X names and creates X more form inputs meant for each person's specific score. The javascript then creates the following variables upon the clicking of the names/scores form's submit button:

我有一个网页,最初有两个表单输入,一个是名字列表,另一个是游戏标题。我已经编写了一些javascript/jquery,它们使用X名称并为每个人的特定分数创建更多的表单输入。然后,javascript在单击名称/分数表单的提交按钮时创建以下变量:

gameTitle = Monopoly
users = [Bob, Bill, Jim, Janet]
scores = [100, 110, 90, 80]
positions = [2, 1, 3, 4]

I then have a MongoDB schema set up as such:

然后我建立了MongoDB模式如下:

const SessionSchema = new mongoose.Schema({
gameTitle: String,
users: [],
scores: [],
positions: []
});

And a Node.js handler as such:

和一个节点。js处理程序是这样的:

const express = require('express');
const router = express.Router();
const bodyParser = require('body-parser');
const timestamps = require('mongoose-timestamp');

router.use(bodyParser.urlencoded({ extended: true }));
router.use(bodyParser.json());

const Session = require('./Session');

//Post a session to the database
router.post('/', function(req, res) {
    Session.create({
        gameTitle : req.body.gameTitle,
        users : req.body.user,
        scores : req.body.score,
        positions : req.body.position
    },
    function (err, session) {
        if (err) return res.status(500).send("There was a problem adding the information to the database");
        res.status(200).send(session);
    });
});

Using Postman I can see that posting works when I use this format:

使用邮差,我可以看到张贴工作时,我使用这个格式:

Postman POST

邮递员的帖子

Postman GET

邮递员会

How do I take the created javascript variables and, also upon the clicking of the names/scores form's submit button, POST them through the API and into the MongoDB database?

如何使用创建的javascript变量,以及单击name /scores表单的submit按钮,通过API将它们发布到MongoDB数据库中?

Apologies if I have missed any important information/code - I haven't fully wrapped my head around how the backend stuff works.

如果我错过了任何重要的信息/代码,我很抱歉——我还没有完全理解后台的工作原理。

2 个解决方案

#1


1

You need to register your Schema:

您需要注册您的模式:

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

const SessionSchema = new mongoose.Schema({
   gameTitle: String,
   users: [],
   scores: [],
   positions: []
});

module.exports = mongoose.model('session', SessionSchema);

And here you need to use the mongo schema model, like this:

这里你需要使用mongo模式模型,像这样:

const express = require('express');
const router = express.Router();
const bodyParser = require('body-parser');
const timestamps = require('mongoose-timestamp');

router.use(bodyParser.urlencoded({ extended: true }));
router.use(bodyParser.json());


const SessionSchema  = require('./Session'); // register the mongo model

const mongoose = require('mongoose');
const Session  = mongoose.model('session');

//Post a session to the database
router.post('/', function(req, res) {
    const new_session = {
        gameTitle : req.body.gameTitle,
        users : req.body.user,
        scores : req.body.score,
        positions : req.body.position
    };


    new_session.save((err, saved_session) => {
        if(err) {
            res.json(err);
        } else {
            res.json(saved_session);
        }
    });

});

更多相关文章

  1. Javascript实现统一的表单验证
  2. 【JavaScript】案例一:使用JS完成注册页面表单校验
  3. 如何检查不包含提交按钮的HTML5表单的有效性?
  4. Django -表单无效但没有错误
  5. 如何在/account / url模式之外使用Django-AllAuth注册表单?
  6. 【JavaWeb-6】HttpServletResponse的字符字节输出流、编码、文件
  7. web基础之自动处理表单填装javabean
  8. ***100分,谁有用java mail做的把表单直接发送到邮箱的网页例子,发

随机推荐

  1. ARMv8(aarch64)页表建立过程详细分析
  2. linux下打乱txt文件的行序
  3. 关于主机远程唤醒(WOL,Wake on Lan)的几种
  4. qte4.8.5配置和移植
  5. Linux Device和Driver注册过程,以及Probe
  6. MySQL备份与还原(一)
  7. Linux安装SVN服务器
  8. linux文件目录权限和系统基础优化命令
  9. 如何卸载内核代码中的文件系统
  10. 我的电脑认为有符号整数比-1小?