快递APP如何将用户从服务器端重定向到:id路径上

问题描述 投票:0回答:1

我正在学习Node.js,我发现将用户重定向到一个:id路径上有些麻烦。我想在那里打印他的用户名。所以,为了做一个概述,它是一个登陆页面,有一个表单,我要求提供一个别名和一个电子邮件。当用户点击提交时,我想把他移动到 :id 路径来打印其用户名。我的代码如下。

var express  = require("express"),
    app      = express(),
    request  = require("request"),
    mongoose = require("mongoose"),
    bodyParser = require("body-parser");

app.set("view engine", "ejs")
app.use(express.static("public"));
app.use(bodyParser.urlencoded({extended: true}));

mongoose.connect("mongodb://localhost/Scape_Room", {useNewUrlParser: true, useUnifiedTopology: true});
var userSchema = new mongoose.Schema({
    email: String,
    alias: String,
});

var user = mongoose.model ("user",userSchema);

app.get("/", function(req, res){
    res.render("index")
})

app.post("/newuser", function(req,res){
    var name = req.body.name;
    var email = req.body.email;
    var newUser = {name:name, email:email}
    user.create(newUser, function(err,newlyUser){
        if(err){
           console.log(err)
           } else {
               res.redirect("/start/:id")
           }
    })
})

app.get("/start/:id", function(req,res){
    user.findById(req.params.id, function(err, foundUser){
        if(err){
            console.log(err)
        } else{
            res.render("startPoint", {user:foundUser})
        }
    })
})

app.listen(3000, function(err){
        console.log("Server listening")
})

error如下: { CastError: Cast to ObjectId failed for value ":id" at path "_id" for model "user" (用户)

我试过:-把路径改成:_id-在路径中加入start。

node.js mongodb express
1个回答
1
投票

当你使用 redirect() 方法,你必须传递一个真实的路由url而不是id。

用node.js的最佳实践来完成代码。


const express = require("express")
const request = require("request");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");

const app = express();

app.set("view engine", "ejs")
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));

mongoose.connect("mongodb://localhost/Scape_Room", { useNewUrlParser: true, useUnifiedTopology: true });

const userSchema = new mongoose.Schema({
  email: String,
  alias: String,
});

const user = mongoose.model("user", userSchema);

app.get("/", function (req, res) {
  res.render("index");
});

app.post("/newuser", function (req, res) {
  const { name, email } = req.body;
  if (!name || !email) {
    // here you must handle missing req.body. lets log console for now
    console.log("Name - Email missing!");
    return res.end();
  }

  const newUser = { name: name, email: email }
  user.create(newUser, function (err, newlyUser) {
    if (err) {
      console.log(err);
      return res.end();
    }

    if (!newlyUser) {
      console.log("Couldn't save user!");
      return res.end();
    }

    if (!newlyUser.id) {
      console.log("No user id found");
      return res.end();
    }

    res.redirect(`/start/${newlyUser.id}`);
  });
});

app.get("/start/:id", function (req, res) {
  user.findById(req.params.id, function (err, user) {
    if (err) {
      console.log(err);
    } else {
      res.render("startPoint", { user });
    }
  })
})

app.listen(3000, function (err) {
  console.log("Server listening")
});



1
投票

使用类似下面的代码

app.post("/newuser", function(req,res){
    var name = req.body.name;
    var email = req.body.email;
    var newUser = {name:name, email:email}
    user.create(newUser, function(err,newlyUser){
        if(err){
           console.log(err)
           } else {
               res.redirect(`/start/${newlyUser._id}`)
           }
    })
})
热门问题
推荐问题
最新问题