I’m learning Node.js and I’m finding some troubles with redirecting the user to an :id path. I would like to print there his username. So to make an overview it is a landing page with a form where I ask for an Alias and an email. When user clicks submit I’d like to move him to /:id path to print its’ username. My code is the following:
JavaScript
x
49
49
1
var express = require("express"),
2
app = express(),
3
request = require("request"),
4
mongoose = require("mongoose"),
5
bodyParser = require("body-parser");
6
7
app.set("view engine", "ejs")
8
app.use(express.static("public"));
9
app.use(bodyParser.urlencoded({extended: true}));
10
11
mongoose.connect("mongodb://localhost/Scape_Room", {useNewUrlParser: true, useUnifiedTopology: true});
12
var userSchema = new mongoose.Schema({
13
email: String,
14
alias: String,
15
});
16
17
var user = mongoose.model ("user",userSchema);
18
19
app.get("/", function(req, res){
20
res.render("index")
21
})
22
23
app.post("/newuser", function(req,res){
24
var name = req.body.name;
25
var email = req.body.email;
26
var newUser = {name:name, email:email}
27
user.create(newUser, function(err,newlyUser){
28
if(err){
29
console.log(err)
30
} else {
31
res.redirect("/start/:id")
32
}
33
})
34
})
35
36
app.get("/start/:id", function(req,res){
37
user.findById(req.params.id, function(err, foundUser){
38
if(err){
39
console.log(err)
40
} else{
41
res.render("startPoint", {user:foundUser})
42
}
43
})
44
})
45
46
app.listen(3000, function(err){
47
console.log("Server listening")
48
})
49
error is the following: { CastError: Cast to ObjectId failed for value “:id” at path “_id” for model “user”
I’ve tried: – to change the path to :_id – added start/ into the route
Advertisement
Answer
When you are using the redirect()
method, you must pass a real route url not an id.
JavaScript
1
65
65
1
const express = require("express")
2
const request = require("request");
3
const mongoose = require("mongoose");
4
const bodyParser = require("body-parser");
5
const app = express();
6
7
app.set("view engine", "ejs")
8
app.use(express.static("public"));
9
10
mongoose.connect("mongodb://localhost/Scape_Room", { useNewUrlParser: true, useUnifiedTopology: true });
11
12
const userSchema = new mongoose.Schema({
13
email: String,
14
alias: String,
15
});
16
17
const user = mongoose.model("user", userSchema);
18
19
app.get("/", function (req, res) {
20
res.render("index");
21
});
22
23
app.post("/newuser", function (req, res) {
24
const { name, email } = req.body;
25
if (!name || !email) {
26
return res.end();
27
}
28
29
const newUser = { name: name, email: email }
30
user.create(newUser, function (err, newlyUser) {
31
if (err) {
32
console.log(err);
33
return res.end();
34
}
35
36
if (!newlyUser) {
37
console.log("Couldn't save user!");
38
return res.end();
39
}
40
41
if (!newlyUser.id) {
42
console.log("No user id found");
43
return res.end();
44
}
45
46
res.redirect(`/start/${newlyUser.id}`);
47
});
48
});
49
50
app.get("/start/:id", function (req, res) {
51
user.findById(req.params.id, function (err, user) {
52
if (err) {
53
console.log(err);
54
} else {
55
res.render("startPoint", { user });
56
}
57
})
58
})
59
60
app.listen(3000, function (err) {
61
console.log("Server listening")
62
});
63
64
65