6

I have posted data like _attachments variable: enter image description here

I want to prepare that data to insert as the following structure:

"_attachments": [
  {
    "container": "string",
    "fileName": "string",
    "name": "string",
    "mime": "string",
    "size": 0
  }
]

But what i have done:

for(let key in _attachments) {
  job._attachments[key]['container']  = _attachments[key]['container'];
  job._attachments[key]['fileName']  = _attachments[key]['fileName'];
  job._attachments[key]['name']      = _attachments[key]['name'];
  job._attachments[key]['mime']      = _attachments[key]['mime'];
  job._attachments[key]['size']      = _attachments[key]['size'];
}

give this error:

 Unprocessable Entity

Note: I'm using loopback.

3
  • Unprocessable entity at? Mongodb side? Commented Mar 9, 2017 at 7:29
  • @Aravind When i post data to api for inserting this error occurre. Commented Mar 9, 2017 at 7:30
  • have you checked it by setting job._attachments = _attachments directly ? As per your object keys, i think you don't need to loop through all '_attachments' Commented Mar 9, 2017 at 7:36

4 Answers 4

4
+50

Just add this to your attachment.josn:

"id": {
  "type": "string",
  "id": true,
  "defaultFn": "uuid"
}

and no need to loop the data.

Sign up to request clarification or add additional context in comments.

Comments

3

From the screen shot _attachments seems to be an array; if that is that case you should not use for...in to iterate over it but for..of. for..in will return all enumerable properties including potentially "unwanted" ones

See the bottom of this excellent MDN resource for details (for..of is available in Typescript)

for (let index of _attachments) {
...
}

Even better, use a map

const result  = _attachments.map( att => ...)

Also the structure you map to seem identical to the original structure, why not use a direct assignment ?

Comments

0

You need to Stringify it.

Code

data = { 
    container: "Stuff"
}

var jsonString = JSON.stringify(data); 

console.log(jsonString);

Before

enter image description here

After

enter image description here

This is more what your data looks like,

enter image description here

2 Comments

Still shows the same problem when i want to insert data
But if i upload single file, my insertion is working.
0

JSON.stringify will help you to get object in json format and JSON.parse will convert back from JSON to object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.