Friday, January 18, 2019

MongoDB: Validation if value is empty


Def : If any key's value is empty then check validation



CODE
===================================
from flask import Flask
from flask import jsonify
from flask import request
from flask_pymongo import PyMongo
from bson.json_util import dumps

app = Flask(__name__)

app.config['MONGO_DBNAME'] = 'esawal'
app.config['MONGO_URI'] = 'mongodb://localhost:27017/esawal'

mongo = PyMongo(app)


@app.route('/', methods=['POST'])
def add_star():
  try:
    myDB = mongo.db["customers"]

    if not request.json['name']:
      return jsonify({'responseCoe':400, 'message': 'Name is Empty'})
    if not request.json['address']:
      return jsonify({'responseCode':400, 'message': 'Address is Empty'})

    name = request.json['name']
    address = request.json['address']
    star_id = myDB.insert_one({'name': name, 'address': address})
    return jsonify({'result' : "Success"})
  except Exception:
      return dumps({'error': "400"})

if __name__ == '__main__':
    app.run(debug=True)
===================================

No comments:

Post a Comment