When I run my server and send the form nickname to the database, my program crashes! It's telling me it's because it needs to be an object. it runs fine if I change the req.body.pickname to just req.body but then the data I need to save doesn't get saved. Is there a way to turn this into an object, or to make this work?
server.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)
});
index.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>
This is the error I get if that helps.
ObjectParameterError: Parameter "obj" to Document() must be an object, got
redditaza
at new ObjectParameterError (C:\Users\10FGD\node_modules\mongoose\lib\error\objectParameter.js:25:11)
at model.Document (C:\Users\10FGD\node_modules\mongoose\lib\document.js:89:11)
at model.Model (C:\Users\10FGD\node_modules\mongoose\lib\model.js:104:12)
at new model (C:\Users\10FGD\node_modules\mongoose\lib\model.js:4656:15)
at app.post (C:\Users\10FGD\Desktop\Games\doubleTheTrouble\server.js:27:26)
at Layer.handle [as handle_request] (C:\Users\10FGD\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\10FGD\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\10FGD\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\10FGD\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\10FGD\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\Users\10FGD\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\10FGD\node_modules\express\lib\router\index.js:275:10)
at serveStatic (C:\Users\10FGD\node_modules\express\node_modules\serve-static\index.js:75:16)
at Layer.handle [as handle_request] (C:\Users\10FGD\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\10FGD\node_modules\express\lib\router\index.js:317:13)
at C:\Users\10FGD\node_modules\express\lib\router\index.js:284:7
I should note that the code that's in my console.log() works just fine, and that's what I would like to have sent to my database.
this seemed to work incase anyone in the future has the same problem. i used mongodb instead of mongoose require
But if you have a solution that works with mongoose please post it :)
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", {
useNewUrlParser: true
});
var db = mongoose.connection;
db.once('open', function(cb) {
console.log("connection established");
})
app.post("/addname", (req, res) =>{
var name = req.body;
var data = {
"nickname": name
}
db.collection('wowww').insertOne(data, function(err, coll) {
if(err) throw err;
console.log('rec estab');
});
});
app.listen(PORT, function () {
console.log("server is up and running using port " + PORT)
});
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 Middleware
app.use(cors());
// Logger Middleware
app.use(morgan('dev'));
// Bodyparser Middleware
app.use(bodyParser.json());
const MongoClient = require('mongodb').MongoClient;
const uri = "uri";
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
client.connect(err => {
console.log('MongoDB Connected...')
const collection = client.db("dbname").collection("collectionname");
app.post('/name', (req, res) => {
collection. insertOne({ name: req.body.name })
res.send("data added")
});
});
const port = process.env.PORT || 5000;
app.listen(port, function () {
console.log(`Example app listening on port ${port}`);
});
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.