Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

node.js - 推送功能后未定义的对象标题(Undefined object title after push function)

I am learning to code using express js but I encountered a problem today, le me show you some code:

(我正在学习使用Express JS进行编码,但今天遇到了一个问题,请教给我一些代码:)

      //main js file

      const express=require('express');

      const app=express();

      const path=require('path');

      const dirRoot =require('./util/path')

      const bodyParser=require('body-parser');

      const adminData = require('./routes/admin.js');
      const shopRoute= require('./routes/shop.js');




        app.use(express.static(path.join(__dirname , 'public')));

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

        app.use('/admin',adminData.routes);

       app.use(shopRoute);

       app.listen(2000);





//----------------
//admin.js
const products=[];

router.get('/add-product',(req,res,next)=>{
res.sendFile(path.join(dirRoot, 'views', 'home.html'));

 //<form action="/admin/add-product" method="post" >
 //   <input name ="title" type="text">
 //   <button>Send</button>
 // </form>                           <-- this form is contained in home.html

});

router.post('/add-product',(req,res,next)=>{
products.push({title:req.body.title});
res.redirect('/');
});

 exports.routes=router;

 exports.products=products;

 //-----------------------
 //shop.js

 const adminData=require('./admin.js');


  router.get('/',(req,res,next)=>{
  console.log(adminData.products);
  res.sendFile(path.join(dirRoot,'views','success.html'))
  });

  module.exports=router;

Everything seems to work properly but in the console I am not being able to display the user text input, I get just [ { title: undefined } ].

(一切似乎都正常运行,但是在控制台中我无法显示用户文本输入,我只得到了[{title:undefined}]。)

I also tried removing the .title specification from req.body, and it looks like this: [ { title: [Object: null prototype] { inserimento: 'bedduxsdsdsds' } } ]

(我还尝试了从req.body中删除.title规范,它看起来像这样:[{title:[Object:null prototype] {inserimento:'bedduxsdsdsdsds'}}])

in eiher ways there is something wrong that I am doing, could anyone spot what is being incorrect?

(以某种方式,我在做错事,有人可以找出不正确的地方吗?)

Thanks in advance.

(提前致谢。)

  ask by Marc Abdel Wahed translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...