[Vuejs]-Inserting a record to MongoDB from an axios post

0👍

I had to convert it to a php object first

<?php
require 'vendor/autoload.php';

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Max-Age: 1000");
header("Access-Control-Allow-Headers: X-Requested-With, Content-Type, Origin, Cache-Control, Pragma, Authorization, Accept, Accept-Encoding");
header("Access-Control-Allow-Methods: PUT, POST, GET, OPTIONS, DELETE");

// takes raw data from the request 
$json = file_get_contents('php://input');
// Converts it into a PHP object 
$data = json_decode($json, true);

$client = new MongoDB\Client("mongodb://localhost:27017");
$collection = $client->dev->customers;

$collection->insertOne($data);

?>

Leave a comment