Here's a JavaScript code that uses the Parse REST API to create a "Car" class and populate it with sample data of 10 different car models and brands:
// Initialize Parse
Parse.initialize("YOUR_APP_ID", "YOUR_JAVASCRIPT_KEY");
Parse.serverURL = "https://YOUR_PARSE_SERVER_URL/parse";
// Create the Car class
const Car = Parse.Object.extend("Car");
// Sample car data
const carData = [
{ brand: "Toyota", model: "Camry", mileage: 25000, color: "Silver", fabricationDate: new Date(2019, 0, 1) },
{ brand: "Honda", model: "Civic", mileage: 30000, color: "Blue", fabricationDate: new Date(2018, 5, 15) },
{ brand: "Ford", model: "Mustang", mileage: 15000, color: "Red", fabricationDate: new Date(2020, 2, 10) },
{ brand: "Chevrolet", model: "Corvette", mileage: 8000, color: "Yellow", fabricationDate: new Date(2021, 8, 20) },
{ brand: "BMW", model: "X5", mileage: 20000, color: "Black", fabricationDate: new Date(2017, 11, 5) },
{ brand: "Mercedes-Benz", model: "C-Class", mileage: 18000, color: "White", fabricationDate: new Date(2019, 6, 30) },
{ brand: "Audi", model: "A4", mileage: 22000, color: "Gray", fabricationDate: new Date(2018, 9, 12) },
{ brand: "Lexus", model: "RX", mileage: 28000, color: "Pearl White", fabricationDate: new Date(2016, 3, 25) },
{ brand: "Nissan", model: "Altima", mileage: 35000, color: "Green", fabricationDate: new Date(2017, 7, 8) },
{ brand: "Tesla", model: "Model 3", mileage: 10000, color: "Midnight Silver", fabricationDate: new Date(2020, 10, 18) }
];
// Function to save car data to Parse
function saveCarData(carData) {
const car = new Car();
car.set("brand", carData.brand);
car.set("model", carData.model);
car.set("mileage", carData.mileage);
car.set("color", carData.color);
car.set("fabricationDate", carData.fabricationDate);
return car.save();
}
// Save car data to Parse
Promise.all(carData.map(saveCarData))
.then(() => {
console.log("Car data saved successfully!");
})
.catch((error) => {
console.error("Error saving car data:", error);
});
Before running this code, make sure to replace "YOUR_APP_ID", "YOUR_JAVASCRIPT_KEY", and "https://YOUR_PARSE_SERVER_URL/parse" with your actual Parse app ID, JavaScript key, and Parse server URL, respectively.
This code does the following:
-
It initializes the Parse SDK with your app ID, JavaScript key, and server URL.
-
It creates a new Parse class called "Car" using Parse.Object.extend("Car").
-
It defines an array carData that contains sample data for 10 different car models and brands. Each car object has fields for brand, model, mileage, color, and fabrication date.
-
It defines a function saveCarData that takes a car object and saves it to the Parse database. The function creates a new instance of the "Car" class, sets the field values using the set method, and then saves the car object using the save method.
-
It uses Promise.all and map to save all the car data to the Parse database by calling the saveCarData function for each car object in the carData array.
-
Finally, it logs a success message if the car data is saved successfully or an error message if there's an error during the saving process.
After running this code, the "Car" class will be created in your Parse database, and it will be populated with the sample data of 10 different car models and brands.