NodeJS MongoDB崩溃“参数必须是对象”

当我运行我的服务器并将表单昵称发送到数据库时,我的程序崩溃了!它告诉我这是因为它需要成为一个对象。如果我将 req.body.pickname 更改为 req.body ,它运行良好,但是我需要保存的数据不会被保存。有没有办法把它变成一个对象,或者让它工作?


服务器.js


var mongoose = require("mongoose");

var bodyParser = require("body-parser");

var express = require("express");

var app = express();


app.use(bodyParser.json());

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




var PORT = 45050;


app.use("/", express.static(__dirname));

mongoose.Promise = global.Promise;

mongoose.connect("mongodb://localhost/thisworks", {

    useNewUrlParser: true

});



var gameSchema = new mongoose.Schema({

    nickname: String

});

var User = mongoose.model("wowww", gameSchema);



app.post("/addname", (req, res) =>{

    var playerNickname = new User(req.body.pickname);

    playerNickname.save()

    .then(item => {

        console.log("nickname created " + req.body.pickname);


    })

    .catch(err => {

        res.status(400).send("unable to save to database");

        console.log("error baby!");

    });

});


app.listen(PORT, function () {

    console.log("server is up and running using port " + PORT)

});

索引.html


<form method="post" action="/addname">

    <h1 class="center-align"> <input id="pickName" class="center-align" type='text' name='pickname' placeholder='Nickname' required> </h1>

    <h1 class='center-align'><input id='rea2dy' value=" Ready >" type='submit'></h1>               

 </form>

如果有帮助,这就是我得到的错误。


狐的传说
浏览 141回答 2
2回答

慕妹3146593

const express = require('express');var mongoose = require('mongoose');const bodyParser = require('body-parser');var morgan = require('morgan');var cors = require('cors')const app = express();// CORS Middlewareapp.use(cors());// Logger Middlewareapp.use(morgan('dev'));// Bodyparser Middlewareapp.use(bodyParser.json());const MongoClient = require('mongodb').MongoClient;const uri = "uri";const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });client.connect(err => {&nbsp; console.log('MongoDB Connected...')&nbsp; const collection = client.db("dbname").collection("collectionname");&nbsp; app.post('/name', (req, res) => {&nbsp; &nbsp; collection. insertOne({ name: req.body.name })&nbsp; &nbsp; res.send("data added")&nbsp; });});const port = process.env.PORT || 5000;app.listen(port, function () {&nbsp; console.log(`Example app listening on port ${port}`);});

慕后森

这似乎可行,以防将来有人遇到同样的问题。我使用 mongodb 而不是 mongoose 要求但是如果你有一个适用于猫鼬的解决方案,请发布它:)var mongoose = require("mongoose");var bodyParser = require("body-parser");var express = require("express");var app = express();var PORT = 3332;app.use("/", express.static(__dirname));app.use(bodyParser.json());app.use(bodyParser.urlencoded({extended: true}));mongoose.connect("mongodb://localhost/thisworks", {&nbsp; &nbsp; useNewUrlParser: true});&nbsp; &nbsp;var db = mongoose.connection;&nbsp; &nbsp;db.once('open', function(cb) {&nbsp; &nbsp;console.log("connection established");})app.post("/addname", (req, res) =>{&nbsp; &nbsp; var name = req.body;&nbsp; &nbsp; var data = {&nbsp; &nbsp; &nbsp; &nbsp; "nickname": name&nbsp; &nbsp; }&nbsp; &nbsp; db.collection('wowww').insertOne(data, function(err, coll) {&nbsp; &nbsp; &nbsp; &nbsp; if(err) throw err;&nbsp; &nbsp; &nbsp; &nbsp; console.log('rec estab');&nbsp; &nbsp; });});app.listen(PORT, function () {&nbsp; &nbsp; console.log("server is up and running using port " + PORT)});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript