Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.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 为什么数组作为字符串返回_Javascript_Vue.js_Mongoose Schema - Fatal编程技术网

Javascript 为什么数组作为字符串返回

Javascript 为什么数组作为字符串返回,javascript,vue.js,mongoose-schema,Javascript,Vue.js,Mongoose Schema,create.vue组件提交一个数组,其中chartLabels的数据属性定义为数组 data() { return { report: { licenseUsage: { chartLabels: [], } ... Mongoose模式将数据类型定义为数组 const ReportSchema = Schema( { licenseUsage: { chartLabels: Array,

create.vue组件提交一个数组,其中chartLabels的数据属性定义为数组

  data() {
    return {
      report: {
        licenseUsage: {
          chartLabels: [],
}
...
Mongoose模式将数据类型定义为数组

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)
      });
    },
    },
如果我检查控制台,输入字段类型的形式是数组

const ReportSchema = Schema(
    {
      licenseUsage: {
        chartLabels: Array,
      },

只要我在表单中单击并键入3、50、56、53、3、4,就会检查控制台是否更改为字符串

axios以数组中的字符串形式返回数据

["3, 50, 56, 53, 3, 4", __ob__: Observer]
0: "3, 50, 56, 53, 3, 4"
为什么我得到一个字符串数组?我希望得到6个这样的项目

Array(6)
0: 3
1: 50
2: 56
3: 53
4: 3
5: 4
谢谢你的帮助

--编辑

如果数组是硬编码的,而不是像这样定义为空数组,则返回一个包含6项的数组

chartLabels: [3, 50, 56, 53, 3, 4]

也许问题是如何从单个表单输入字段返回数组?

尝试JSON.decode()您的响应这是因为当您输入[cit.:]表单并键入3、50、56、53、3、4时,给了它一个字符串

除非另有规定,否则所有输入数据默认为字符串,并且必须显式执行

在HTML5之前的版本中,您只能输入/获取字符串类型的数据。现在,在许多其他新的输入属性中,您可以拥有:
,但没有像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的人来说,苹果都有答案——问题是在喂养怪物之前应该在哪里分开。API中的.split也是这样。现在,在创建时以字符串或数字形式返回数组

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)
        }

axios以字符串形式返回数据
。。。那么,你是说
response.data
是一个stringyes响应。data是一个字符串,但需要是一个arrayso,你的代码失败了,对吗?在你的api实现之后,很可能你只是返回了一个字符串。api是在上面添加的@appleapple and Learn it数组是硬编码的-定义为默认值-返回的项目数组。我试图使用单个输入表单字段来获取数据,很抱歉,在PHP中,没有
JSON.decode()
,而是
JSON\u decode()
。要将这些字符串转换为数字,只需
“3,50,56,53,3,4”。拆分(',')。映射(数字)这很好,但是,也许他不需要数组成员的类型为number