【问题标题】:How to get data from POST request如何从 POST 请求中获取数据
【发布时间】:2017-09-02 15:08:48
【问题描述】:

我很困惑如何从发布请求中获取数据,然后将其发送到工作表。我不知道如何与客户确认。任何帮助将不胜感激,谢谢。

我正在尝试构建一个网络应用程序,它将成为一个登记/结帐系统,一旦有人扫描条形码,来自该条形码的信息将直接进入谷歌电子表格

var express = require('express');//takes everything from the Express library
var app = express(); //calls the express constructor. app is the server object
var fs = require('fs');
var GoogleSpreadsheet = require('google-spreadsheet');
var creds = require('./client_secret.json');


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

app.get('/', function (req, res) {
    var contents = fs.readFileSync('views/welcome.html').toString();
    res.send(contents);
});

app.get('/info', function (req, res) {
    res.send('Hello Info!');
});

app.get('/checkin', function (req, res) {
    var contents = fs.readFileSync('views/no.html').toString();
    res.send(contents);
    console.log("checking in");
    console.log(req);
});

app.get('/checkout', function (req, res) {
    var contents = fs.readFileSync('views/no.html').toString();
    res.send(contents);
    console.log("checking out");
});

app.post('/checkin', function (req, res){ //someone clicks submit on page from 
get page
    console.log("posting checkin info");


    //getthe data from the POST request LOOOK HERE GET INFO FROM POST REQUEST
    //send it to Sheets
//body-parser method
var ID = data.ID; // sample
//console.log(id);

// Create a document object using the ID of the spreadsheet - obtained from 
its URL.
var doc = new 
GoogleSpreadsheet(________);

// Authenticate with the Google Spreadsheets API.
doc.useServiceAccountAuth(creds, function (err) {

/*  // Get all of the rows from the spreadsheet.
  doc.getRows(2, function (err, rows) {
    console.log(rows);
  });*/

  doc.getInfo(function(err, info) {
  console.log('Loaded doc: '+info.title+' by '+info.author.email);
  sheet = info.worksheets[0];
  console.log('sheet 1: '+sheet.title+' '+sheet.rowCount+'x'+sheet.colCount);
 // step();

   //look at https://www.npmjs.com/package/google-spreadsheet
});
});

//confirm with the client
});

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

【问题讨论】:

    标签: javascript json node.js express google-spreadsheet-api


    【解决方案1】:

    要获取POST 数据,您需要使用body-parser 包。

    导入:

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

    你可以这样使用它:

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

    最后在你的中间件中,你可以像这样访问数据:

    var ID = req.body.ID;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-15
      • 2020-03-15
      • 2016-11-17
      • 1970-01-01
      • 1970-01-01
      • 2019-11-26
      • 2016-03-09
      相关资源
      最近更新 更多