在json文件中使用map方法创建调查问题

[英]create survey issue when using map in json file for POST method


async createSurvey(streamId, title, specialization, description, 
answers) {
let token = await getToken();
let url = '/api/survey';
let answer_text;
 answers.map((answer, index) => {
  answer_text = answer.answer;
  const body = JSON.stringify({
    question: {
      title: title,
      description: description,
      specialization: specialization,
      answers_attributes: {
        '0': {
          answer_text: answer_text
        }
      }
    }
  });
  return fetch(BASE_URL + url, {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
      'Authorization': 'Token token=' + token
    },
    body
  }).then(res => {
   console.log('server response :', res);
    return res;
  }).catch(err => console.log('Fetch error :', err));
})
}

I am using this code, the map is for answers_attributes and so it creates survey same as the number of answers.length. so I want to avoid duplication. and I want to add map only for answers_attribute.please suggest me any solution for this.

我正在使用此代码,该地图用于answers_attributes,因此它创建的调查与answers.length的数量相同。所以我想避免重复。我想为answers_attribute添加地图。请给我任何解决方案。

edited:

when i pass the data :

当我传递数据时:

title: 'Api testing', description: 'api description', specialization: 'Api specialization', answers =[{answer_text: "answer1"}, {answer_text: "answer2 "}]

标题:'Api测试',描述:'api描述',专业化:'Api专业化',答案= [{answer_text:“answer1”},{answer_text:“answer2”}]

i am getting output:
title: 'Api testing', description: 'api description', specialization: 'Api specialization', answers =[{answer_text: "answer1"}]

我得到输出:标题:'Api测试',描述:'api描述',专业化:'Api专业化',答案= [{answer_text:“answer1”}]

title: 'Api testing', description: 'api description', specialization: 'Api specialization', answers =[{answer_text: "answer2 "}]

标题:'Api测试',描述:'api描述',专业化:'Api专业化',答案= [{answer_text:“answer2”}]

but I want output:

但我想要输出:

title: 'Api testing', description: 'api description', specialization: 'Api specialization', answers =[0: {answer_text: "answer1"}, 1: {answer_text: "answer2 "}]

标题:'Api测试',描述:'api描述',专长:'Api专长',答案= [0:{answer_text:“answer1”},1:{answer_text:“answer2”}]

So that means I am getting the whole data answers.length number of time. which is my issue.

这意味着我得到了整个数据的答案。时间长度。这是我的问题。

1 个解决方案

#1


1  

You could use map directly to map to answer_attributes, no need to map every answer to an individual object to send to the server. Try this:

您可以直接使用map映射到answer_attributes,无需将每个答案映射到单个对象以发送到服务器。试试这个:

async createSurvey(streamId, title, specialization, description, answers) {
  let token = await getToken();
  let url = '/api/survey';

  const body = JSON.stringify({
    question: {
      title: title,
      description: description,
      specialization: specialization,
      answers_attributes: answers.map(answer => ({ answer_text: answer.answer }))
    }
  });

  return fetch(BASE_URL + url, {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
      'Authorization': 'Token token=' + token
    },
    body
  }).then(res => {
    console.log('server response :', res);
    return res;
  }).catch(err => console.log('Fetch error :', err));
}
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2018/01/09/d099084473433842f6c2fc7b174e5e85.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告