【问题标题】:Express URL param issues with Nested array in NodeJSNodeJS中嵌套数组的表达URL参数问题
【发布时间】:2019-03-25 08:53:15
【问题描述】:

我在通过对 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: []
};

这是通过 http 请求发送数据以使用 Axios 表达的示例。关系的那一面工作得很好,因为我在我的 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());

提前感谢您的帮助。

【问题讨论】:

  • 您应该将您的答案标记为已接受
  • 哦,谢谢,我想我做到了。

标签: javascript node.js express multidimensional-array query-parameters


【解决方案1】:

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

首先,您有一个名为querystring 的局部变量,它与节点模块变量冲突。我不确定为什么您没有收到错误,因为您使用 const 作为节点模块 querystring

反正解析参数的正确方法是:

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

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

【讨论】:

  • 原来我的问题出在原来的电话里。我对其进行了测试,当我发现调用问题时,我的代码确实有效。但是,您的语法是正确的并且工作得很好。我正在使用两个模块来做他们都可以做的一件事,所以我只是按照你的建议进行了切换。谢谢。
【解决方案2】:

已修复。所以我是个菜鸟。我在我认为不是的地方发现了问题,即最初的 Axios 调用。

在创建该调用时,我使用以下格式来设置我的数据,这些数据在您到达数组之前可以正常工作。

cp = {
property_id: '',
property_notes: '',
property_features: []
};

我正在将数据推送到数组中。

    for (i = 0; i < active.length; i++) {
    curfea.push(active[i].id);
    }

当我拨打 Axios 电话时,我遇到了问题。它将数据作为字符串发送,但所有其他数据都可以。那时我意识到我正在处理一个 Axios 对象,所以我对数据如何传输的想法是错误的。

    async function creaprop() {
    console.log('Creating Property.......');
    try {
        const propfeatures = await axios(theUrl, { params: {
            acc: 'cnp',
            property_features: JSON.stringify(curfea),
            property_notes: scp.property_notes
        } });
        console.log('Async Request: Add New Property');
    } catch (error) {
        console.error(error);
    }
   }

在发送数据之前对数据运行一个简单的字符串化可以解决我的所有问题。我遇到了对象问题,而不是 Express 或参数问题。

【讨论】:

    猜你喜欢
    • 2020-11-28
    • 1970-01-01
    • 2017-07-01
    • 2016-04-16
    • 2015-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-27
    相关资源
    最近更新 更多