Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript NodeJS中嵌套数组的Express URL参数问题_Javascript_Node.js_Express_Multidimensional Array_Query Parameters - Fatal编程技术网

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

Javascript NodeJS中嵌套数组的Express URL参数问题,javascript,node.js,express,multidimensional-array,query-parameters,Javascript,Node.js,Express,Multidimensional Array,Query Parameters,我在通过AXIOS对Express的调用将数据从阵列发送到API时遇到问题。电话发送的很好,但我在下面提供。请求接收良好,请求中的所有其他数据都插入到数据库中,没有问题。问题是我无法获取正在发送的属性参数中的数据。我在调用中看到,我得到了多个属性参数,但我不知道如何获取数据 console.log('Logging Features' + params.property_features); 显示未定义,但控制台中的查询显示我获得了数据 app.all('/', function(req, r

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

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!!!!');
});
如您所见,我正在接收一个带有params的请求,并对其进行解析,然后通过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请求发送到express的数据示例。关系的这一方面工作得很好,因为我正在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参数获取多个数据段,因此在请求中我看到属性_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
的局部变量,它与节点模块变量冲突。我不确定您为什么没有收到错误,因为您对节点模块
querystring
使用了
const

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

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

如果url中有多个
property\u功能
变量,将它们放入一个数组中,这也可以处理这种情况。

已修复。所以我是个笨蛋。我在我认为不是的地方发现了问题,原来的Axios调用

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

cp = {
property_id: '',
property_notes: '',
property_features: []
};
    for (i = 0; i < active.length; i++) {
    curfea.push(active[i].id);
    }
我正在将数据推送到数组中

cp = {
property_id: '',
property_notes: '',
property_features: []
};
    for (i = 0; i < active.length; i++) {
    curfea.push(active[i].id);
    }

在发送数据之前对数据运行一个简单的stringify修复了我所有的问题。我遇到的是对象问题,而不是表达式或参数问题

原来我的问题是在最初的通话中。我对它进行了测试,当我发现调用有问题时,我的代码确实起了作用。但是,您的语法是正确的,并且工作良好。我用了两个模块来做一件他们都能做的事情,所以我只是按照你的建议把它切换了。谢谢你。你应该把你的答案标记为已接受。