猿问

如何在Javascript中将地图推送到列表中

所以我想做的是从表单中获取数据并将其作为地图推送到我的列表中。它目前说


无法读取未定义的属性“url”


express = require("express");

app = express();

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

app.use(express.static("public"));

var imagedata = [

    {url: "...", description: "..."},

    {url: "...", description: "..."}

];

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



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

    res.render("home.ejs", {imagedata: imagedata});

});

app.post("/post", function(req, req){

    var NewPost = req.body.url;

    var Description = req.body.description;

    imagedata.push({url: NewPost, description: Description});

    res.redirect("/");


});


喵喵时光机
浏览 136回答 3
3回答

慕妹3242003

说req.body是未定义的,因为您将请求和响应都定义为req更改app.post("/post", function(req, req)为app.post("/post", function(req, res)

一只萌萌小番薯

在没有看到客户端代码(表单)的情况下,我真的看不出代码有什么问题。尝试添加console.log到/post:express = require("express");app = express();var bodyParser = require("body-parser");app.use(express.static("public"));var imagedata = [    {url: "...", description: "..."},    {url: "...", description: "..."}];app.use(bodyParser.urlencoded({extended: true}));app.get("/", function(req, res){    res.render("home.ejs", {imagedata: imagedata});});app.post("/post", function(req, req){    var NewPost = req.body.url;    var Description = req.body.description;    console.log("NewPost:", NewPost, "NewDescription:", Description);    imagedata.push({url: NewPost, description: Description});    console.log("imagedata:", imagedata);    res.redirect("/");});查看正在提供哪些数据以及发生了什么imagedata。

繁星coding

如前所述,这个问题并不是很清楚,但也许这就是您要寻找的东西,尽管我并没有真正看到在这种情况下使用地图而不是对象的用例。在此处查看有关何时使用地图的帖子express = require("express");app = express();var bodyParser = require("body-parser");app.use(express.static("public"));var imagedata = [    {url: "...", description: "..."},    {url: "...", description: "..."}];app.use(bodyParser.urlencoded({extended: true}));app.get("/", function(req, res){    res.render("home.ejs", {imagedata: imagedata});});app.post("/post", function(req, req){    var NewPost = req.body.url;    var Description = req.body.description;    let map = new Map()     map.set('url', NewPost).set('description', Description)    imagedata.push(map);    res.redirect("/");});
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答