I'm trying to build a website for a school project. I setted up everything but i get the error "Data.map is not a function". Actually i'm not an expert and i'm just trying to understand and copy the code that the teacher gave us (we did almost nothing in node.js).
I've built the server using node.js
const express = require("express");
const app = express();
const process = require("process");
const bodyParser = require("body-parser");
let serverPort = process.env.PORT || 5000;
app.use(express.static(__dirname + "/public"));
app.set("port", serverPort);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/doctors', function(req,res){
var config = require('./doctor.json');
res.send(JSON.stringify(config));
})
/* Start the server on port 3000 */
app.listen(serverPort, function() {
console.log(`Your app is ready at port ${serverPort}`);
});
When i connect to the doctor page, i load a JSON file with all the doctors and i send it back to the client. Here is the JSON
[{
"id": 0,
"born": 2010,
"tag": "cat",
"name": "Eleanore"
}, {
"id": 1,
"born": 2010,
"tag": "dog",
"name": "Katelin"
}, {
"id": 2,
"born": 2012,
"tag": "dog",
"name": "Shea"
}, {
"id": 3,
"born": 2011,
"tag": "cat",
"name": "Stephen"
}, {
"id": 4,
"born": 2011,
"tag": "cat",
"name": "Rosanne"
}, {
"id": 5,
"born": 2011,
"tag": "cat",
"name": "Alexa"
}
]
Then i load the JSON and i would like to create dynamically items. Here is my client side code:
$.get('/doctors', function (data) {
$("<div class='row' id='dottori' ></div>").appendTo(".team");
data.map(addElement);
});
function addElement(doctor){
console.log("Adding doctor");
$("#dottori").append('<p> '+doctor.name+'</p>');
I keep getting this error, even if i stringify the json file (the same thing that my professor did), you can find the original code here: https://bitbucket.org/polimi-hyp-2017-10173412/polimi-hyp-2018-project/src/master/