【问题标题】:Mongoose validation CastError: Cast to string failed for valueMongoose 验证 CastError: Cast to string failed for value
【发布时间】:2020-10-21 04:40:30
【问题描述】:

我正在尝试在节点中运行我的 .js 脚本,但是在数据库中添加一些新数据时,服务器未加载并给出此错误(CastError: Cast to string failed for value)。我没有收到错误,有人可以帮我吗?附上 cmd 的 SS 和代码![在此处输入图像描述][1]

Code is as follows:


var express=require("express");
var app= express();
var bodyParser=require("body-parser");
app.use(bodyParser.urlencoded({extended:true}));
app.set("view engine","ejs");
var mongoose=require("mongoose");

mongoose.set("useNewUrlParser",  true);
mongoose.set("useUnifiedTopology",true);
mongoose.connect("mongodb://localhost/yelp_camp");

var campgroundsschema= new mongoose.Schema({
    name:String,
    image:String
});

var Campground= mongoose.model("Campground",campgroundsschema);


//Campground.create(
  //  {
    //  name:"Granite Hill",
      //image:"https://images.unsplash.com/photo-1487750404521-0bc4682c48c5?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60"
    //},function(err,campgrounds){
      //if(err){
       //   console.log(err);
      //}
      //else{
       //   console.log("We have created a new campground");
        //  console.log(campgrounds);
      //}
    //}
    //)


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


app.get("/campgrounds",function(req,res){
     
     Campground.find({},function(err,allCampgrounds){
        if(err){
            console.log(err);
        }
        else{
            res.render("campgrounds",{campgrounds:allCampgrounds});
        }
     });
});

app.post("/campgrounds",function(req,res){
  var name=req.body.name;
  var image=req.body.image;
  var newCampground={name: name,image: image};
  Campground.create(newCampground,function(err,newlyCreated){
    if(err){
        console.log(err);
    }
    else{
        console.log("we have created a new campground here!!");
          res.redirect("/campgrounds");
        }
  });
});

app.get("/campgrounds/new",function(req,res){
     res.render("newcamp.ejs");

 });


var port = process.env.PORT || 3000;

app.listen(port, function () {
  console.log('Example app listening on port ' + port + '!');
});


Error ScreenShot:  [1]: https://i.stack.imgur.com/9vNPz.png

(请打开此链接查看图片)

【问题讨论】:

    标签: node.js mongodb express mongoose web-development-server


    【解决方案1】:

    查看您在正文中附加的图片链接。我可以看到您正在发送一个字符串数组,看起来像这样

    [ "C1", "A URL" ]

    这实际上是一个数组数据类型,而您在架构中为 name 设置的类型是 String

    如果你想保存一个字符串数组?您必须将数据类型更改为字符串数组,即

    name: [String]

    或者你可以对你发送的数组进行字符串化。使用

    JSON.stringify(yourArray)

    This 是 mongoose 数据类型的 url,因此您也可以探索其他数据类型。

    更新

    var image = JSON.stringify(req.body.image);

    在上面的代码块中,我对图像数组进行了字符串化。然后我尝试保存它并使其正常工作。

    【讨论】:

    • 非常感谢您的帮助!我尝试这样做,但错误仍然存​​在
    • @SOHAMBHAT 您是否尝试对要保存的数组进行字符串化?因为我只是在我身边做了它并且它正在工作。您可以实现数组数据类型。如果你想将数组存储在字符串中?然后你应该对数组进行字符串化。
    • 是的,我将数据类型更改为数组,并且我也对数组进行了字符串化,现在代码正在运行,但我想在我的 html 页面上显示名称和图像,但没有发生!!谢谢您的回复
    • 感谢您通过更改数据类型和对图像数组进行排序来解决这么多错误!!!
    • 我很高兴它有帮助。你现在能接受这个答案是正确的吗?
    猜你喜欢
    • 1970-01-01
    • 2021-01-07
    • 2015-08-10
    • 2017-03-23
    • 2022-08-23
    • 2017-08-14
    • 2017-02-06
    • 1970-01-01
    • 2019-02-17
    相关资源
    最近更新 更多