Flask TypeError: argument of type 'NoneType' is not iterable










3














I am not sure why I am getting this TypeError:




File "C:/Users/PycharmProjects/REST/app.py", line 30, in
valid_book_object
if ("isbn" in book and "name" in book and "price" in book):
TypeError: argument of type 'NoneType' is not iterable
127.0.0.1 - - [12/Nov/2018 14:22:29] "POST /books HTTP/1.1" 500 -




Code:



from flask import Flask, jsonify, request
from test import *
app=Flask(__name__)

books=[
'name': 'M',
'price': 6.75,
'isbn':123
,
'name': 'G',
'price': 7.75,
'isbn':456
,

]


#GET /store
@app.route('/books') #first endpoint
def get_books():
return jsonify('books': books)

# POST /books
#'name': 'F',
#'price': 7.00,
#'isbn': 789
#,

def valid_book_object(book):
if ("isbn" in book and "name" in book and "price" in book):
return True
print("true")
else:
return False
print("false")


@app.route('/books', methods=['POST'])
def add_book():
#return jsonify(request.get_json())
request_data=request.get_json()
if(valid_book_object(request_data)):
books.insert(0, request_data)
return "True"
else:
return "False"


#GET /books/456
@app.route('/books/<int:isbn>') #second endpoint
def get_book_by_isbn(isbn):
return_value=
for book in books:
if book["isbn"]==isbn:
return_value=
'name': book["name"],
'price':book["price"]

return jsonify(return_value)



app.run(port=5000)









share|improve this question























  • book seems to have the value None. So when you try to do "isbn" in book, book is assumed to be an iterable but instead it is found to be None which is not an iterable. That is what the error message says.
    – Eskapp
    Nov 13 '18 at 15:24










  • Is it a must to define book class?? books is used as an argument in code.
    – LPGA
    Nov 13 '18 at 18:39










  • It is not related to having a class or not. It is related to a value (None) that the variable book can take and that is not handled in the code. Did you check whether request_data is an empty json file or not when the error occurs.
    – Eskapp
    Nov 13 '18 at 19:27















3














I am not sure why I am getting this TypeError:




File "C:/Users/PycharmProjects/REST/app.py", line 30, in
valid_book_object
if ("isbn" in book and "name" in book and "price" in book):
TypeError: argument of type 'NoneType' is not iterable
127.0.0.1 - - [12/Nov/2018 14:22:29] "POST /books HTTP/1.1" 500 -




Code:



from flask import Flask, jsonify, request
from test import *
app=Flask(__name__)

books=[
'name': 'M',
'price': 6.75,
'isbn':123
,
'name': 'G',
'price': 7.75,
'isbn':456
,

]


#GET /store
@app.route('/books') #first endpoint
def get_books():
return jsonify('books': books)

# POST /books
#'name': 'F',
#'price': 7.00,
#'isbn': 789
#,

def valid_book_object(book):
if ("isbn" in book and "name" in book and "price" in book):
return True
print("true")
else:
return False
print("false")


@app.route('/books', methods=['POST'])
def add_book():
#return jsonify(request.get_json())
request_data=request.get_json()
if(valid_book_object(request_data)):
books.insert(0, request_data)
return "True"
else:
return "False"


#GET /books/456
@app.route('/books/<int:isbn>') #second endpoint
def get_book_by_isbn(isbn):
return_value=
for book in books:
if book["isbn"]==isbn:
return_value=
'name': book["name"],
'price':book["price"]

return jsonify(return_value)



app.run(port=5000)









share|improve this question























  • book seems to have the value None. So when you try to do "isbn" in book, book is assumed to be an iterable but instead it is found to be None which is not an iterable. That is what the error message says.
    – Eskapp
    Nov 13 '18 at 15:24










  • Is it a must to define book class?? books is used as an argument in code.
    – LPGA
    Nov 13 '18 at 18:39










  • It is not related to having a class or not. It is related to a value (None) that the variable book can take and that is not handled in the code. Did you check whether request_data is an empty json file or not when the error occurs.
    – Eskapp
    Nov 13 '18 at 19:27













3












3








3


1





I am not sure why I am getting this TypeError:




File "C:/Users/PycharmProjects/REST/app.py", line 30, in
valid_book_object
if ("isbn" in book and "name" in book and "price" in book):
TypeError: argument of type 'NoneType' is not iterable
127.0.0.1 - - [12/Nov/2018 14:22:29] "POST /books HTTP/1.1" 500 -




Code:



from flask import Flask, jsonify, request
from test import *
app=Flask(__name__)

books=[
'name': 'M',
'price': 6.75,
'isbn':123
,
'name': 'G',
'price': 7.75,
'isbn':456
,

]


#GET /store
@app.route('/books') #first endpoint
def get_books():
return jsonify('books': books)

# POST /books
#'name': 'F',
#'price': 7.00,
#'isbn': 789
#,

def valid_book_object(book):
if ("isbn" in book and "name" in book and "price" in book):
return True
print("true")
else:
return False
print("false")


@app.route('/books', methods=['POST'])
def add_book():
#return jsonify(request.get_json())
request_data=request.get_json()
if(valid_book_object(request_data)):
books.insert(0, request_data)
return "True"
else:
return "False"


#GET /books/456
@app.route('/books/<int:isbn>') #second endpoint
def get_book_by_isbn(isbn):
return_value=
for book in books:
if book["isbn"]==isbn:
return_value=
'name': book["name"],
'price':book["price"]

return jsonify(return_value)



app.run(port=5000)









share|improve this question















I am not sure why I am getting this TypeError:




File "C:/Users/PycharmProjects/REST/app.py", line 30, in
valid_book_object
if ("isbn" in book and "name" in book and "price" in book):
TypeError: argument of type 'NoneType' is not iterable
127.0.0.1 - - [12/Nov/2018 14:22:29] "POST /books HTTP/1.1" 500 -




Code:



from flask import Flask, jsonify, request
from test import *
app=Flask(__name__)

books=[
'name': 'M',
'price': 6.75,
'isbn':123
,
'name': 'G',
'price': 7.75,
'isbn':456
,

]


#GET /store
@app.route('/books') #first endpoint
def get_books():
return jsonify('books': books)

# POST /books
#'name': 'F',
#'price': 7.00,
#'isbn': 789
#,

def valid_book_object(book):
if ("isbn" in book and "name" in book and "price" in book):
return True
print("true")
else:
return False
print("false")


@app.route('/books', methods=['POST'])
def add_book():
#return jsonify(request.get_json())
request_data=request.get_json()
if(valid_book_object(request_data)):
books.insert(0, request_data)
return "True"
else:
return "False"


#GET /books/456
@app.route('/books/<int:isbn>') #second endpoint
def get_book_by_isbn(isbn):
return_value=
for book in books:
if book["isbn"]==isbn:
return_value=
'name': book["name"],
'price':book["price"]

return jsonify(return_value)



app.run(port=5000)






python-3.x pycharm flask-restful






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 22:55









Trooper Z

753424




753424










asked Nov 12 '18 at 20:31









LPGA

439




439











  • book seems to have the value None. So when you try to do "isbn" in book, book is assumed to be an iterable but instead it is found to be None which is not an iterable. That is what the error message says.
    – Eskapp
    Nov 13 '18 at 15:24










  • Is it a must to define book class?? books is used as an argument in code.
    – LPGA
    Nov 13 '18 at 18:39










  • It is not related to having a class or not. It is related to a value (None) that the variable book can take and that is not handled in the code. Did you check whether request_data is an empty json file or not when the error occurs.
    – Eskapp
    Nov 13 '18 at 19:27
















  • book seems to have the value None. So when you try to do "isbn" in book, book is assumed to be an iterable but instead it is found to be None which is not an iterable. That is what the error message says.
    – Eskapp
    Nov 13 '18 at 15:24










  • Is it a must to define book class?? books is used as an argument in code.
    – LPGA
    Nov 13 '18 at 18:39










  • It is not related to having a class or not. It is related to a value (None) that the variable book can take and that is not handled in the code. Did you check whether request_data is an empty json file or not when the error occurs.
    – Eskapp
    Nov 13 '18 at 19:27















book seems to have the value None. So when you try to do "isbn" in book, book is assumed to be an iterable but instead it is found to be None which is not an iterable. That is what the error message says.
– Eskapp
Nov 13 '18 at 15:24




book seems to have the value None. So when you try to do "isbn" in book, book is assumed to be an iterable but instead it is found to be None which is not an iterable. That is what the error message says.
– Eskapp
Nov 13 '18 at 15:24












Is it a must to define book class?? books is used as an argument in code.
– LPGA
Nov 13 '18 at 18:39




Is it a must to define book class?? books is used as an argument in code.
– LPGA
Nov 13 '18 at 18:39












It is not related to having a class or not. It is related to a value (None) that the variable book can take and that is not handled in the code. Did you check whether request_data is an empty json file or not when the error occurs.
– Eskapp
Nov 13 '18 at 19:27




It is not related to having a class or not. It is related to a value (None) that the variable book can take and that is not handled in the code. Did you check whether request_data is an empty json file or not when the error occurs.
– Eskapp
Nov 13 '18 at 19:27












1 Answer
1






active

oldest

votes


















0














You are not sending JSON data to /books route using POST method.



I tried your solution with postman and it worked. Also, if you want to use some route for GET and POST, don't split them. Use methods=['GET', 'POST']. Here is your code reformatted with PEP 8 standard:



from flask import Flask, jsonify, request

app = Flask(__name__)

books = [
'name': 'M',
'price': 6.75,
'isbn': 123
,
'name': 'G',
'price': 7.75,
'isbn': 456

]

# POST /books
#
# "name": "F",
# "price": 7.00,
# "isbn": 789
#


def valid_book_object(book):
if "isbn" in book and "name" in book and "price" in book:
return True
else:
return False


@app.route('/books', methods=['GET', 'POST'])
def add_book():
# If request is GET, just return JSON data of books.
if request.method == 'GET':
return jsonify('books': books)
else:
# This is part if it is POST request
request_data = request.get_json()
if valid_book_object(request_data):
books.insert(0, request_data)
return "True"
else:
return "False"


# GET /books/456
@app.route('/books/<int:isbn>') # second endpoint
def get_book_by_isbn(isbn):
return_value =
for book in books:
if book["isbn"] == isbn:
return_value =
'name': book["name"],
'price': book["price"]

return jsonify(return_value)
return 'No book with isbn value'.format(isbn)


if __name__ == '__main__':
app.run(port=5000)


And here is the output from postman (you can see True at the bottom, that is what you return if POST was successful):



postman



If you use postman, be sure to select application/json content-type.



If you are using JQuery ajax method, please read this answer. But anyway, here is using JQuery (tested):



data = JSON.stringify(
name: "F",
price: 7.00,
isbn: 789
);
$.ajax(
url: '/books',
type: "POST",
data: data,
contentType: "application/json",
dataType: "json",
success: function()
console.log('Post executed successfully');

)





share|improve this answer
















  • 1




    yes I was not trailing with /books for POSTMAN
    – LPGA
    Dec 13 '18 at 4:19










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269650%2fflask-typeerror-argument-of-type-nonetype-is-not-iterable%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You are not sending JSON data to /books route using POST method.



I tried your solution with postman and it worked. Also, if you want to use some route for GET and POST, don't split them. Use methods=['GET', 'POST']. Here is your code reformatted with PEP 8 standard:



from flask import Flask, jsonify, request

app = Flask(__name__)

books = [
'name': 'M',
'price': 6.75,
'isbn': 123
,
'name': 'G',
'price': 7.75,
'isbn': 456

]

# POST /books
#
# "name": "F",
# "price": 7.00,
# "isbn": 789
#


def valid_book_object(book):
if "isbn" in book and "name" in book and "price" in book:
return True
else:
return False


@app.route('/books', methods=['GET', 'POST'])
def add_book():
# If request is GET, just return JSON data of books.
if request.method == 'GET':
return jsonify('books': books)
else:
# This is part if it is POST request
request_data = request.get_json()
if valid_book_object(request_data):
books.insert(0, request_data)
return "True"
else:
return "False"


# GET /books/456
@app.route('/books/<int:isbn>') # second endpoint
def get_book_by_isbn(isbn):
return_value =
for book in books:
if book["isbn"] == isbn:
return_value =
'name': book["name"],
'price': book["price"]

return jsonify(return_value)
return 'No book with isbn value'.format(isbn)


if __name__ == '__main__':
app.run(port=5000)


And here is the output from postman (you can see True at the bottom, that is what you return if POST was successful):



postman



If you use postman, be sure to select application/json content-type.



If you are using JQuery ajax method, please read this answer. But anyway, here is using JQuery (tested):



data = JSON.stringify(
name: "F",
price: 7.00,
isbn: 789
);
$.ajax(
url: '/books',
type: "POST",
data: data,
contentType: "application/json",
dataType: "json",
success: function()
console.log('Post executed successfully');

)





share|improve this answer
















  • 1




    yes I was not trailing with /books for POSTMAN
    – LPGA
    Dec 13 '18 at 4:19















0














You are not sending JSON data to /books route using POST method.



I tried your solution with postman and it worked. Also, if you want to use some route for GET and POST, don't split them. Use methods=['GET', 'POST']. Here is your code reformatted with PEP 8 standard:



from flask import Flask, jsonify, request

app = Flask(__name__)

books = [
'name': 'M',
'price': 6.75,
'isbn': 123
,
'name': 'G',
'price': 7.75,
'isbn': 456

]

# POST /books
#
# "name": "F",
# "price": 7.00,
# "isbn": 789
#


def valid_book_object(book):
if "isbn" in book and "name" in book and "price" in book:
return True
else:
return False


@app.route('/books', methods=['GET', 'POST'])
def add_book():
# If request is GET, just return JSON data of books.
if request.method == 'GET':
return jsonify('books': books)
else:
# This is part if it is POST request
request_data = request.get_json()
if valid_book_object(request_data):
books.insert(0, request_data)
return "True"
else:
return "False"


# GET /books/456
@app.route('/books/<int:isbn>') # second endpoint
def get_book_by_isbn(isbn):
return_value =
for book in books:
if book["isbn"] == isbn:
return_value =
'name': book["name"],
'price': book["price"]

return jsonify(return_value)
return 'No book with isbn value'.format(isbn)


if __name__ == '__main__':
app.run(port=5000)


And here is the output from postman (you can see True at the bottom, that is what you return if POST was successful):



postman



If you use postman, be sure to select application/json content-type.



If you are using JQuery ajax method, please read this answer. But anyway, here is using JQuery (tested):



data = JSON.stringify(
name: "F",
price: 7.00,
isbn: 789
);
$.ajax(
url: '/books',
type: "POST",
data: data,
contentType: "application/json",
dataType: "json",
success: function()
console.log('Post executed successfully');

)





share|improve this answer
















  • 1




    yes I was not trailing with /books for POSTMAN
    – LPGA
    Dec 13 '18 at 4:19













0












0








0






You are not sending JSON data to /books route using POST method.



I tried your solution with postman and it worked. Also, if you want to use some route for GET and POST, don't split them. Use methods=['GET', 'POST']. Here is your code reformatted with PEP 8 standard:



from flask import Flask, jsonify, request

app = Flask(__name__)

books = [
'name': 'M',
'price': 6.75,
'isbn': 123
,
'name': 'G',
'price': 7.75,
'isbn': 456

]

# POST /books
#
# "name": "F",
# "price": 7.00,
# "isbn": 789
#


def valid_book_object(book):
if "isbn" in book and "name" in book and "price" in book:
return True
else:
return False


@app.route('/books', methods=['GET', 'POST'])
def add_book():
# If request is GET, just return JSON data of books.
if request.method == 'GET':
return jsonify('books': books)
else:
# This is part if it is POST request
request_data = request.get_json()
if valid_book_object(request_data):
books.insert(0, request_data)
return "True"
else:
return "False"


# GET /books/456
@app.route('/books/<int:isbn>') # second endpoint
def get_book_by_isbn(isbn):
return_value =
for book in books:
if book["isbn"] == isbn:
return_value =
'name': book["name"],
'price': book["price"]

return jsonify(return_value)
return 'No book with isbn value'.format(isbn)


if __name__ == '__main__':
app.run(port=5000)


And here is the output from postman (you can see True at the bottom, that is what you return if POST was successful):



postman



If you use postman, be sure to select application/json content-type.



If you are using JQuery ajax method, please read this answer. But anyway, here is using JQuery (tested):



data = JSON.stringify(
name: "F",
price: 7.00,
isbn: 789
);
$.ajax(
url: '/books',
type: "POST",
data: data,
contentType: "application/json",
dataType: "json",
success: function()
console.log('Post executed successfully');

)





share|improve this answer












You are not sending JSON data to /books route using POST method.



I tried your solution with postman and it worked. Also, if you want to use some route for GET and POST, don't split them. Use methods=['GET', 'POST']. Here is your code reformatted with PEP 8 standard:



from flask import Flask, jsonify, request

app = Flask(__name__)

books = [
'name': 'M',
'price': 6.75,
'isbn': 123
,
'name': 'G',
'price': 7.75,
'isbn': 456

]

# POST /books
#
# "name": "F",
# "price": 7.00,
# "isbn": 789
#


def valid_book_object(book):
if "isbn" in book and "name" in book and "price" in book:
return True
else:
return False


@app.route('/books', methods=['GET', 'POST'])
def add_book():
# If request is GET, just return JSON data of books.
if request.method == 'GET':
return jsonify('books': books)
else:
# This is part if it is POST request
request_data = request.get_json()
if valid_book_object(request_data):
books.insert(0, request_data)
return "True"
else:
return "False"


# GET /books/456
@app.route('/books/<int:isbn>') # second endpoint
def get_book_by_isbn(isbn):
return_value =
for book in books:
if book["isbn"] == isbn:
return_value =
'name': book["name"],
'price': book["price"]

return jsonify(return_value)
return 'No book with isbn value'.format(isbn)


if __name__ == '__main__':
app.run(port=5000)


And here is the output from postman (you can see True at the bottom, that is what you return if POST was successful):



postman



If you use postman, be sure to select application/json content-type.



If you are using JQuery ajax method, please read this answer. But anyway, here is using JQuery (tested):



data = JSON.stringify(
name: "F",
price: 7.00,
isbn: 789
);
$.ajax(
url: '/books',
type: "POST",
data: data,
contentType: "application/json",
dataType: "json",
success: function()
console.log('Post executed successfully');

)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 20:30









Dinko Pehar

1,0472324




1,0472324







  • 1




    yes I was not trailing with /books for POSTMAN
    – LPGA
    Dec 13 '18 at 4:19












  • 1




    yes I was not trailing with /books for POSTMAN
    – LPGA
    Dec 13 '18 at 4:19







1




1




yes I was not trailing with /books for POSTMAN
– LPGA
Dec 13 '18 at 4:19




yes I was not trailing with /books for POSTMAN
– LPGA
Dec 13 '18 at 4:19

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269650%2fflask-typeerror-argument-of-type-nonetype-is-not-iterable%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線