为什么数组返回为字符串
why is array returned as string
create.vue组件提交一个数组,其中chartLabels的数据属性被定义为一个数组。
data() {
return {
report: {
licenseUsage: {
chartLabels: [],
}
...
Mongoose Schema 将数据类型定义为数组
const ReportSchema = Schema(
{
licenseUsage: {
chartLabels: Array,
},
API 创建报告是
app.post("/api/create", (req, res) => {
const report = new Report({
licenseUsage: {
chartLabels: req.body.licenseUsage.chartLabels,
}
...
});
report.save(err => {
if (err) return res.status(404).send({ message: err.message });
return res.send({ report });
});
使用axios的get请求是
created: function() {
this.getReport();
},
methods: {
getReport() {
axios.get("http://127.0.0.1:8000/api/report/" + this.$route.params.id).then(response => {
this.report = response.data.report
const { chartLabels } = response.data.report.licenseUsage
this.chartLabels = chartLabels
console.log(chartLabels)
});
},
},
如果我检查控制台,输入字段类型是一个数组
只要我在表单中单击并键入 3、50、56、53、3、4,就会检查控制台对字符串的更改
axios return将数据作为数组中的字符串
["3, 50, 56, 53, 3, 4", __ob__: Observer]
0: "3, 50, 56, 53, 3, 4"
为什么我得到一个包含 1 个字符串的数组?我希望像这样得到一个包含 6 个项目的数组。
Array(6)
0: 3
1: 50
2: 56
3: 53
4: 3
5: 4
感谢您的帮助
--编辑
如果数组是硬编码的,而不是像这样定义为空数组,那么它可以工作,一个包含 6 个项目的数组是 returned。
chartLabels: [3, 50, 56, 53, 3, 4]
也许问题是如何 return 来自单个表单输入字段的数组?
尝试 JSON.decode() 您的回复
这是因为当您输入 [cit.:] "form and type 3, 50, 56, 53, 3, 4" 时,您 给它 一个字符串 值。 =12=]
除非另有说明,否则所有输入数据默认为字符串,并且必须显式完成!
在HTML 5之前,字符串类型的数据就可以了put/get。现在,在许多其他新的输入属性中,您可以拥有:<input type="number">
,但没有 input type = "array".
这样的东西
因此,您需要将输入数据转换为数组对象,然后再将其喂给怪物。
最简单的方法是:
"3, 50, 56, 53, 3, 4".split(",");
>> [ 3, 50, 56, 53, 3, 4 ]
//WARNING: each 'number' will be a string of course!
对于任何可能找到这个的人来说 @window.document & @apple apple 有答案 - 问题是在喂食怪物之前在哪里分裂。 .split 像这样进入 API 。现在 returns 数组在创建时作为字符串或数字。
app.post("/api/create", (req, res) => {
const report = new Report({
month: req.body.month,
licenseUsage: {
chartLabels: req.body.licenseUsage.chartLabels.split(','),
nyRevit: req.body.licenseUsage.nyRevit.split(',').map(Number)
}
但是 在更新中不工作 - 错误是 .spit 不是一个函数,因为返回的是一个数组而不是一个字符串 - 但是当更新被保存时它会变回一个字符串。要解决这个问题,您必须先添加 .toString
app.put("/api/report/update/:id", (req, res) => {
Report.findByIdAndUpdate(
req.params.id,
{ $set:
{
licenseUsage: {
chartLabels: req.body.licenseUsage.chartLabels.toString.split(','),
nyRevit: req.body.licenseUsage.nyRevit.toString.split(',').map(Number)
}
create.vue组件提交一个数组,其中chartLabels的数据属性被定义为一个数组。
data() {
return {
report: {
licenseUsage: {
chartLabels: [],
}
...
Mongoose Schema 将数据类型定义为数组
const ReportSchema = Schema(
{
licenseUsage: {
chartLabels: Array,
},
API 创建报告是
app.post("/api/create", (req, res) => {
const report = new Report({
licenseUsage: {
chartLabels: req.body.licenseUsage.chartLabels,
}
...
});
report.save(err => {
if (err) return res.status(404).send({ message: err.message });
return res.send({ report });
});
使用axios的get请求是
created: function() {
this.getReport();
},
methods: {
getReport() {
axios.get("http://127.0.0.1:8000/api/report/" + this.$route.params.id).then(response => {
this.report = response.data.report
const { chartLabels } = response.data.report.licenseUsage
this.chartLabels = chartLabels
console.log(chartLabels)
});
},
},
如果我检查控制台,输入字段类型是一个数组
只要我在表单中单击并键入 3、50、56、53、3、4,就会检查控制台对字符串的更改
axios return将数据作为数组中的字符串
["3, 50, 56, 53, 3, 4", __ob__: Observer]
0: "3, 50, 56, 53, 3, 4"
为什么我得到一个包含 1 个字符串的数组?我希望像这样得到一个包含 6 个项目的数组。
Array(6)
0: 3
1: 50
2: 56
3: 53
4: 3
5: 4
感谢您的帮助
--编辑
如果数组是硬编码的,而不是像这样定义为空数组,那么它可以工作,一个包含 6 个项目的数组是 returned。
chartLabels: [3, 50, 56, 53, 3, 4]
也许问题是如何 return 来自单个表单输入字段的数组?
尝试 JSON.decode() 您的回复
这是因为当您输入 [cit.:] "form and type 3, 50, 56, 53, 3, 4" 时,您 给它 一个字符串 值。 =12=]
除非另有说明,否则所有输入数据默认为字符串,并且必须显式完成!
在HTML 5之前,字符串类型的数据就可以了put/get。现在,在许多其他新的输入属性中,您可以拥有:<input type="number">
,但没有 input type = "array".
因此,您需要将输入数据转换为数组对象,然后再将其喂给怪物。 最简单的方法是:
"3, 50, 56, 53, 3, 4".split(",");
>> [ 3, 50, 56, 53, 3, 4 ]
//WARNING: each 'number' will be a string of course!
对于任何可能找到这个的人来说 @window.document & @apple apple 有答案 - 问题是在喂食怪物之前在哪里分裂。 .split 像这样进入 API 。现在 returns 数组在创建时作为字符串或数字。
app.post("/api/create", (req, res) => {
const report = new Report({
month: req.body.month,
licenseUsage: {
chartLabels: req.body.licenseUsage.chartLabels.split(','),
nyRevit: req.body.licenseUsage.nyRevit.split(',').map(Number)
}
但是 在更新中不工作 - 错误是 .spit 不是一个函数,因为返回的是一个数组而不是一个字符串 - 但是当更新被保存时它会变回一个字符串。要解决这个问题,您必须先添加 .toString
app.put("/api/report/update/:id", (req, res) => {
Report.findByIdAndUpdate(
req.params.id,
{ $set:
{
licenseUsage: {
chartLabels: req.body.licenseUsage.chartLabels.toString.split(','),
nyRevit: req.body.licenseUsage.nyRevit.toString.split(',').map(Number)
}