javascript - NodeJS 中嵌套数组的 Express URL 参数问题

标签 javascript node.js express multidimensional-array query-string

我在通过对 Express 的 AXIOS 调用发送到我的 API 的数组中获取数据时遇到问题。电话发送得很好,但我在下面提供了。请求被正常接收,请求中的所有其他数据都毫无问题地插入到数据库中。问题是我无法获取正在发送的 property_features 参数中的数据。我在通话中看到我收到了多个 property_features 参数,但我不知道如何获取数据。

console.log('Logging Features' + params.property_features);

显示未定义但控制台中的查询显示我已获取数据。

app.all('/', function(req, res) {
    var querystring = url.parse(req.url, true);
    var params = querystring.query;
    //console.log(querystring.search);
    console.log('Logging Features' + params.property_features);
    //Insert Type to populate new property Type field
    if (params.acc === 'ant') {
        console.log('Creating new property type...');
        connection.query('INSERT INTO property_type(types) VALUES(?)', [params.property_type], function(error, results, fields) {
            if (error) throw error;
            console.log('Insert Property Type Query Successful' + results);
        });
        res.send('Property type added OK!!');
        //Insert Property
    } else if (params.acc === 'cnp') {
        console.log('Creating new property....');
        params.property_id = newuuid();
        connection.query('INSERT INTO properties(property_id, branch, reference_number, property_type, property_price, price_option, rooms, bath, date_built, squarefeet, address, location, zip, rental_criteria, short_descrip, full_descrip, property_status, featured, visible, tour_url, property_features, notes) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)', [params.property_id, params.branch, params.reference_number, params.property_type, params.property_price, params.price_option, params.rooms, params.bath, params.date_built, params.squarefeet, params.address, params.location, params.zip, params.rental_criteria, params.short_descrip, params.full_descrip, params.property_status, params.featured, params.visible, params.tour_url, params.property_features, params.property_notes], function(error, results, fields) {
            if (error) throw error;
            console.log('Insert Property Query Successful' + results);
        });
        res.send('Property added OK!!');
        //Insert location to populate new property locations field
    }
    console.log('There was a successful transaction!!!!');
});

正如您所看到的,我正在接收带有参数的请求并解析它们,然后通过 nodejs 中的 mysql 模块将它们插入到数据库中。还有其他一些,但我很确定这是与该模块隔离的。

我遇到的问题是 property_features 参数。

cp = {
    property_id: '',
    branch: document.getElementById('prop_branch').value,
    reference_number: document.getElementById('reference_number').value,
    property_type: document.getElementById('property_type').value,
    property_price: document.getElementById('property_price').value,
    price_option: document.getElementById('price_option').value,
    rooms: document.getElementById('rooms').value,
    bath: document.getElementById('bath').value,
    date_built: document.getElementById('date_built').value,
    squarefeet: document.getElementById('squarefeet').value,
    address: document.getElementById('property_address').value,
    location: document.getElementById('property_location').value,
    zip: document.getElementById('property_zip').value,
    rental_criteria: document.getElementById('rental_criteria').value,
    short_descrip: document.getElementById('short_descrip').value,
    full_descrip: document.getElementById('full_descrip').value,
    property_status: document.getElementById('property_status').value,
    featured: '',
    visible: '',
    tour_url: document.getElementById('tour_url').value,
    property_notes: '',
    property_features: []
};

这是使用 Axios 通过 http 请求发送的数据示例。关系的那一边工作得很好,因为我正在我的 API 中获取数据。

2018-10-19 16:41:05.776 PDT "GET /?acc=cnp&branch=Branch&reference_number=&property_type=Unfurnished&property_price=&price_option=Per+Month&rooms=1&bath=0&date_built=&squarefeet=&address=&location=Long+Beach&zip=&rental_criteria=&short_descrip=&full_descrip=&property_status=Leased&featured=true&visible=true&tour_url=&property_notes=&property_features%5B%5D=Swimming+Pool&property_features%5B%5D=Close+to+Burbank+Elementary&property_features%5B%5D=Lower+Unit&property_features%5B%5D=Laminated+Hardwood+Floors&property_features%5B%5D=Utilities+paid HTTP/1.1" 304 158 - "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36" "centprop-219008.appspot.com" ms=1117 cpu_ms=1274 cpm_usd=1.7657e-8 loading_request=1 instance=00c61b117cf1a1e2d4a42d33a8f29b82f4343c86edd94138399fa3b2d077916b3324cd9246 app_engine_release=1.9.65 trace_id=43a2aabf9b0d5726a3dfb4ba610b5ba2

如您所见,我正在以带有嵌入式数组的 JSON 格式发送数据。那就是我的问题发生的地方。除了此变量中的数据外,所有其他数据都可以很好地填充到数据库中。

axios.get(theUrl, {
    params: {
        acc: 'cnp',
        property_id: cp.id,
        branch: cp.branch,
        reference_number: cp.reference_number,
        property_type: cp.property_type,
        property_price: cp.property_price,
        price_option: cp.price_option,
        rooms: cp.rooms,
        bath: cp.bath,
        date_built: cp.date_built,
        squarefeet: cp.squarefeet,
        address: cp.address,
        location: cp.location,
        zip: cp.zip,
        rental_criteria: cp.rental_criteria,
        short_descrip: cp.short_descrip,
        full_descrip: cp.full_descrip,
        property_status: cp.property_status,
        featured: cp.featured,
        visible: cp.visible,
        tour_url: cp.tour_url,
        property_notes: cp.property_notes,
        property_features: cp.property_features
    }
})
.then(function (response) {
    console.log(response);
})
.catch(function (error) {
    console.log(error);
})
.then(function () {
    // always executed
});

这里是发送数据的 axios 调用,这边再次工作正常,数据确实到达了 API,为了清楚起见,我发布了这个。

2018-10-19 16:43:12.842 PDT Logging Features undefined

这是我在 API 中注销参数时遇到的问题。 features 参数正在获取多条数据,因此在请求中我看到了 property_feature=&!!!多次。

我再次尝试解析数据和字符串化,但我无法在该调用中操作数据。我觉得我缺少一个数组层的地址,我无法理解。

我正在使用的模块。

const http = require('http');
const url = require('url');
const mysql = require('mysql');
const config = require('./config');
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const request = require('request');
const axios = require('axios');
const querystring = require('querystring');
const uuid4 = require('uuid4');
const app = express();
const admin = express(); // the sub app
const secret = express();
const port = 8080
app.use(cors());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

在此先感谢您的帮助。

最佳答案

您错误地使用了 querystring 来获取 url 参数。

首先,您有一个名为 querystring 的局部变量,它与 Node 模块变量冲突。我不确定为什么你在那里没有收到错误,因为你对 Node 模块 querystring 使用了 const

无论如何,解析参数的正确方法是:

const querystring = require("querystring");
var params = querystring.parse(req.url);
console.log(params.property_features);

这还可以处理您的 url 中有多个 property_features 变量的情况,方法是将它们放在一个数组中。

关于javascript - NodeJS 中嵌套数组的 Express URL 参数问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52909723/

相关文章:

javascript - setTimeout函数和异步函数

javascript - Angular 2 和 Express 日期格式 MongoDB

android - Cordova、q.js thorwing e、android 工具在路径中

javascript - Node.js session 文件存储将中止所有后续请求

javascript - mongodb 中带有投影字段的嵌套数组聚合查询?

javascript - 使用 _.template 将 JSON 字符串转换为下划线模板

javascript - 我如何检查图像是否在请求 Node.js 中

node.js - 覆盖 Mongoose 模型方法

java - 'ERR_OSSL_EVP_WRONG_FINAL_BLOCK_LENGTH' 在 Node js 中解密时

node.js - express/node.js 中请求之间的同步问题