I am very new to coding, so my apologies if I use the wrong terms or am unclear. I have a list of 400+ words I need to be able to enter into my database. I found this collection to loop through my data and post it to the database. I am able to mostly get it to work, except for this one section (the meanings section) where it's an array of objects. If it makes a difference, this is for a React project and using MongoDB for the database.
Each word looks like this:
{
"text": "happy",
"traits": [
"wordlist100"
],
"meanings": [
{
"meaning": "happy",
"category": "default"
}
],
"approved": true,
"createdby": "60dcd7ba69b1e52ac8138051",
"approvedBy": "60dcd7ba69b1e52ac8138051",
"lastUserEdit": "60dcd7ba69b1e52ac8138051",
"blocked": false
}
Where each word can have multiple traits and multiple meanings.
I have all the words in a JSON file, and am running it through Postman using the collection linked above. This is the body of my request in Postman:
{
"text": "{{text}}",
"traits": [
"{{traits}}"
],
"meanings": [
{
"meaning": "{{meaning}}",
"category": "{{category}}"
}
],
"approved": true,
"createdby": "60dcd7ba69b1e52ac8138051",
"approvedBy": "60dcd7ba69b1e52ac8138051",
"lastUserEdit": "60dcd7ba69b1e52ac8138051",
"blocked": false
}
Using this, everything comes through correctly in my database except for the meaning and category of a word. This is what shows up in MongoDB:
{
"_id": {"$oid": "6121bd6addff936ba4eb84bf"},
"traits": [
"wordlist100"
],
"text": "happy",
"meanings": [
{
"_id": {"$oid": "6121bd6addff936ba4eb84c0"},
"meaning": "{{meaning}}",
"category": "{{category}}"
}
],
"approved": true,
"createdBy": {"$oid": "60dcd7ba69b1e52ac8138051"},
"approvedBy": {"$oid": "60dcd7ba69b1e52ac8138051"},
"lastUserEdit": {"$oid": "60dcd7ba69b1e52ac8138051"},
"blocked": false,
"createdAt": {"$date": "2021-08-22T02:58:50.679Z"},
"updatedAt": {"$date": "2021-08-22T02:58:50.679Z"},
"__v": 0
}
What am I doing wrong with the meanings section of the word? I tried playing around with several combinations of brackets and such for how to set up the meanings part.
I did try:
"meanings": [
"{{meanings}}"
]
...but that didn't work at all. Most of what I tried created an error that stopped the collection from running at all (it was probably bad syntax, as I said, I'm really new). This is the only arrangement that seems to get me close to right. Any help would be greatly appreciated.
Thanks!!
Attaching pics of the Postman code and created MongoDB document if it's easier for any of you to look at it with that formatting:
So I feel foolish and this just shows how new I am to all of this. When I posted this question, all I had done was to try copying the style of what I saw in the body text of the example I found (linked in my question), but didn't do anything with actually defining variables or touching the pre-request script because I didn't realize that was a separate step. So basically, I just got lucky that it was smart enough to figure out the other values without me defining them as variables.
If anyone is reading this later, here's 3 pages of documentation I used to help me figure out my errors: Using Variables, Importing Data Files, and Scripting in Postman.
From reading those, I added the following code in the pre-request script:
let meanings = pm.iterationData.get("meanings");
pm.variables.set("meaning", meanings[0].meaning);
pm.variables.set("category", meanings[0].category);
I also set "meaning" and "category" as named variables with no initial value in the collection.
Image of adding variables to the Postman collection
One caveat I should note is that I'm not certain how well this will work once I start entering stuff with more than one trait or more than one meaning, because I haven't tested those parts yet. I suspect I still need to tweak things to make it work under those instances. But it is working in the examples I have with just one trait and one object in the meanings array.