Untitled

 avatar
unknown
plain_text
a year ago
1.6 kB
6
Indexable
import connectToMongo from 'src/db'
import { accounts } from 'src/models/Account'
import { loggedAccounts } from 'src/models/LoggedAccount'
import { PythonShell } from 'python-shell'

async function abcd(userId) {
  // Pass userId as an argument to f.py
  const options = {
    args: [userId.toString()]
  }

  PythonShell.run('./f.py', options).then(messages => {
    console.log('finished', { messages })
  })
}

export default async function handler(request, res) {
  connectToMongo()
  const { email, password } = request.body
  if (request.method === 'POST') {
    try {
      // const existingAccountData = await accounts.findOne({ email })
      // if (existingAccountData) {
      //   return res.status(400).json({ message: 'Account Data already exists' })
      // }

      console.log('test')
      const user = request.headers['user-data']
      const userId = JSON.parse(user)._id

      const existingMessage = await loggedAccounts.findOne({
        email: email
      })

       if (existingMessage)
       {
        res.status(201).json({message:'Account already exists'})
       }
       else{

         await abcd(userId)

         const account = new accounts({
           email,
           password,
           user: userId
         })
         const savedAccount = await account.save()

         res.status(201).json({ message: 'Account Data Saved successfully', account: savedAccount })

       }
       
    } catch (error) {
      res.status(500).json({ success: false, error })
    }
  } else {
    res.status(405).json({ error: 'Method Not Allowed' })
  }
}
Editor is loading...
Leave a Comment