0

I want to loop through times, but times is undefined when debugging.

router.post('/something', function (req, res) {
    var times = req.body.times;
    for (i = 0; i < times.length; i++) { 
        // do something
    }
}

Here is what body.times looks like when debugging:

{"duration":3600,"startTime":540,"weekDay":0},{"duration":3600,"startTime":600,"weekDay":0},{"duration":3600,"startTime":660,"weekDay":0},{"duration":3600,"startTime":1080,"weekDay":6}:""

Here's a screenshot of what it looks like in VSCode debugging. Replace spans with times. (I just changed it to times on this question to be more clear.) enter image description here

Here is the swift code:

var jsonArray: [[String: Any]] = [/*The array*/]
let json: [String: Any] = ["times": jsonArray]
guard let body = (try? JSONSerialization.data(withJSONObject: json, 
                                                     options: [])) 
else { 
    return nil 
}

Here is how I configure express:

var app = express();
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
app.use(bodyParser.json({ type: 'application/*+json' }))
app.use(bodyParser.json({ type: 'application/json' }))
app.use(bodyParser.json({ type: 'application/x-www-form-urlencoded'}))

Output of json:

 (lldb) po print(json)
 ["spans": [["weekDay": 0, "duration": 3600.0, "startTime": 540], ["weekDay": 0, "duration": 3600.0, "startTime": 600]]]

The headers enter image description here

7
  • is body.times really an array? it looks like you've got trailing :"" and missing [] Commented Oct 22, 2018 at 5:03
  • Honestly I'm not sure. I'm guessing it's not, but I don't know why. That's the underlying problem I guess. Commented Oct 22, 2018 at 5:04
  • Your Array looks like a more like an object of objects without any key to specify the object than an Array of objects in the spans Commented Oct 22, 2018 at 5:09
  • Your problem is in your swift code. Read this for finding solution Commented Oct 22, 2018 at 5:15
  • Can you put up output of the swift code? the value of jsonArray Commented Oct 22, 2018 at 5:31

1 Answer 1

0

There is two parts to test in your code

  1. Node js
  2. Swift

1.Node js

Test your api with postment check content type (application/json or multipart) and verify its working

2)Check your Swift code and match with content type of server

Your data

{"duration":3600,"startTime":540,"weekDay":0},{"duration":3600,"startTime":600,"weekDay":0},{"duration":3600,"startTime":660,"weekDay":0},{"duration":3600,"startTime":1080,"weekDay":6}:""

you are iterating on object not on array so its wont work.

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

1 Comment

My content type was incorrect. I needed to set it to application/json.

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.