[sqlalchemy] Re: Flask API returns JSON but gives an error in React: Error: Objects are not valid as a React child
I just realized I posted this in sqlalchemy but it's a React issue. Sorry for the confusion. I'll find an appropriate React group somewhere. Dale On Thursday, August 20, 2020 at 1:23:22 PM UTC-5 Dale Preston wrote: > Just more info; if I change res => res.json() to res => res.text(), I get > the json as text. > > *{ "users": [ { "emailaddress": "bobs...@somedomain.com", "firstname": > "Bob", "lastname": "Smith", "userid": "bsmith" }, { "emailaddress": > "tomjones @otherdomain.com <http://otherdomain.com>", "firstname": "Tom", > "lastname": "Jones", "userid": "tjones" }, { "emailaddress": > "carl...@yahoo.com", "firstname": "Carlinda", "lastname": "Gonzalez", > "userid": "cgonz" }, { "emailaddress": "ath...@freemail.com", "firstname": > "Thomas", "lastname": "April", "userid": "athom" } ] } * > > On Thursday, August 20, 2020 at 10:36:42 AM UTC-5 Dale Preston wrote: > >> I have a flask API that returns valid JSON. The Content-Type is >> application/json. I assign it to state using useEffect. When I call on >> the variable returned by useEffect, I get the error: *Error: Objects are >> not valid as a React child*. >> >> I know that React is reading the JSON because I get various details. The >> JSON returns a "users" table. If I assign the JSON to a variable named >> "data" and I try to use "data" I get this detail showing that the users >> table was recognized. >> >> *Error: Objects are not valid as a React child (found: object with keys >> {users}). If you meant to render a collection of children, use an array >> instead. * >> >> If I try to call on data.users, React gives this error showing it >> successfully read the elements in the JSON: >> >> *Error: Objects are not valid as a React child (found: object with keys >> {emailaddress, firstname, lastname, userid}). If you meant to render a >> collection of children, use an array instead. * >> >> Complete details of my scenario, including code, is on StackOverflow at >> https://stackoverflow.com/questions/63498019/objects-are-not-valid-as-a-react-child-or-how-to-consume-json-in-function-reac >> >> Any help in figuring out how to consume JSON from an API is appreciated. >> I have seen a lot of threads and articles on various sites concerning this >> same error but none have helped. >> >> Thanks in advance. >> > -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/e1e88a10-1437-48d5-be66-00eba36dacacn%40googlegroups.com.
[sqlalchemy] Re: Flask API returns JSON but gives an error in React: Error: Objects are not valid as a React child
Just more info; if I change res => res.json() to res => res.text(), I get the json as text. *{ "users": [ { "emailaddress": "bobsm...@somedomain.com", "firstname": "Bob", "lastname": "Smith", "userid": "bsmith" }, { "emailaddress": "tomjones @otherdomain.com", "firstname": "Tom", "lastname": "Jones", "userid": "tjones" }, { "emailaddress": "carli...@yahoo.com", "firstname": "Carlinda", "lastname": "Gonzalez", "userid": "cgonz" }, { "emailaddress": "atho...@freemail.com", "firstname": "Thomas", "lastname": "April", "userid": "athom" } ] } * On Thursday, August 20, 2020 at 10:36:42 AM UTC-5 Dale Preston wrote: > I have a flask API that returns valid JSON. The Content-Type is > application/json. I assign it to state using useEffect. When I call on > the variable returned by useEffect, I get the error: *Error: Objects are > not valid as a React child*. > > I know that React is reading the JSON because I get various details. The > JSON returns a "users" table. If I assign the JSON to a variable named > "data" and I try to use "data" I get this detail showing that the users > table was recognized. > > *Error: Objects are not valid as a React child (found: object with keys > {users}). If you meant to render a collection of children, use an array > instead. * > > If I try to call on data.users, React gives this error showing it > successfully read the elements in the JSON: > > *Error: Objects are not valid as a React child (found: object with keys > {emailaddress, firstname, lastname, userid}). If you meant to render a > collection of children, use an array instead. * > > Complete details of my scenario, including code, is on StackOverflow at > https://stackoverflow.com/questions/63498019/objects-are-not-valid-as-a-react-child-or-how-to-consume-json-in-function-reac > > Any help in figuring out how to consume JSON from an API is appreciated. > I have seen a lot of threads and articles on various sites concerning this > same error but none have helped. > > Thanks in advance. > -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/2a6fa7f5-50a5-4401-9616-eaae2dd24ea3n%40googlegroups.com.
[sqlalchemy] Flask API returns JSON but gives an error in React: Error: Objects are not valid as a React child
I have a flask API that returns valid JSON. The Content-Type is application/json. I assign it to state using useEffect. When I call on the variable returned by useEffect, I get the error: *Error: Objects are not valid as a React child*. I know that React is reading the JSON because I get various details. The JSON returns a "users" table. If I assign the JSON to a variable named "data" and I try to use "data" I get this detail showing that the users table was recognized. *Error: Objects are not valid as a React child (found: object with keys {users}). If you meant to render a collection of children, use an array instead. * If I try to call on data.users, React gives this error showing it successfully read the elements in the JSON: *Error: Objects are not valid as a React child (found: object with keys {emailaddress, firstname, lastname, userid}). If you meant to render a collection of children, use an array instead. * Complete details of my scenario, including code, is on StackOverflow at https://stackoverflow.com/questions/63498019/objects-are-not-valid-as-a-react-child-or-how-to-consume-json-in-function-reac Any help in figuring out how to consume JSON from an API is appreciated. I have seen a lot of threads and articles on various sites concerning this same error but none have helped. Thanks in advance. -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/5951ca49-204e-4839-976b-1132b2153fd9n%40googlegroups.com.
Re: [sqlalchemy] Getting column data from result set with column name as a string.
Thanks. The label is an interesting option; I'll look into that. On a StackOverflow thread, I got *row.__table__.columns* which I can iterate over and test the key, allowing me to get the column I need but I have to loop through all the columns until I find the one I want for each row because columns doesn't have an index either. I also don't like using a private property but I guess (hope) __table__ would always be there. On Tuesday, August 18, 2020 at 6:05:49 PM UTC-5 Mike Bayer wrote: > > > On Tue, Aug 18, 2020, at 5:20 PM, Dale Preston wrote: > > I'm using sqlalchemy 1.3.18. I'm trying to write an app that looks at > data from an ORM declarative table without necessarily knowing the table > definition. > > What I am looking for is a way to get a single object (row in resultSet), > having the name of column[1] is "lastname", and having "lastname" as a > string in memory, how can I get the value of the "lastname" field from the > row in resultSet? > > It's easy if I know in code that the row has a lastname property and I can > use row.lastname but I want to do something like row["lastname"] or > row.columns["lastname"] if there's a way. > > > to get individual columns in the row you query for those columns directly: > > > row = sess.query(User.lastname).first() > > print(row.lastname) > > > otherwise you can always label a column if you need: > > row = sess.query(User.anything.label("lastname")).first() > > print(row.lastname) > > > > > > > > I'm using reflection to get the columns for the table. Here's some code I > tried: > > class Users(Base): > __tablename__ = 'users' > userid = Column(String(80), primary_key=True) > lastname = Column(String(40), nullable=False) > firstname = Column(String(40), nullable=False) > emailaddress = Column(String(80), nullable=False) > > def ReflectTableColumns(DbEngine, meta, targetTable): > > tableschema = Table(targetTable, meta, autoload=True, > autoload_with=DbEngine) > cols = dict() > for c in tableschema.columns: > print("{0}\t|\t{1}".format(c.name, c.type)) > cols[c.name] = c.type > > return cols > > def GetUsers(): > DBSession = sessionmaker(bind=Engine) > session = DBSession() > ShowTableData(session.query(Users).all(), 'users') > > > def ShowTableData(resultSet, tablename): > columns = ReflectTableColumns(Engine, Base.metadata, tablename) > columnNames = list(columns.keys()) > print (type(resultSet)) > for row in resultSet: > print (row.items[columnNames[1]]) > print (row.columns[columnNames[1]]) > print (row[columnNames[1]]) > > GetUsers() > > > -- > SQLAlchemy - > The Python SQL Toolkit and Object Relational Mapper > > http://www.sqlalchemy.org/ > > To post example code, please provide an MCVE: Minimal, Complete, and > Verifiable Example. See http://stackoverflow.com/help/mcve for a full > description. > --- > You received this message because you are subscribed to the Google Groups > "sqlalchemy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to sqlalchemy+...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/sqlalchemy/f11bf60e-e872-489e-9a9b-03998440bbb1n%40googlegroups.com > > <https://groups.google.com/d/msgid/sqlalchemy/f11bf60e-e872-489e-9a9b-03998440bbb1n%40googlegroups.com?utm_medium=email_source=footer> > . > > > -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/975d8626-39cf-439f-b5ed-df1e9680f7bfn%40googlegroups.com.
[sqlalchemy] Getting column data from result set with column name as a string.
I'm using sqlalchemy 1.3.18. I'm trying to write an app that looks at data from an ORM declarative table without necessarily knowing the table definition. What I am looking for is a way to get a single object (row in resultSet), having the name of column[1] is "lastname", and having "lastname" as a string in memory, how can I get the value of the "lastname" field from the row in resultSet? It's easy if I know in code that the row has a lastname property and I can use row.lastname but I want to do something like row["lastname"] or row.columns["lastname"] if there's a way. I'm using reflection to get the columns for the table. Here's some code I tried: class Users(Base): __tablename__ = 'users' userid = Column(String(80), primary_key=True) lastname = Column(String(40), nullable=False) firstname = Column(String(40), nullable=False) emailaddress = Column(String(80), nullable=False) def ReflectTableColumns(DbEngine, meta, targetTable): tableschema = Table(targetTable, meta, autoload=True, autoload_with=DbEngine) cols = dict() for c in tableschema.columns: print("{0}\t|\t{1}".format(c.name, c.type)) cols[c.name] = c.type return cols def GetUsers(): DBSession = sessionmaker(bind=Engine) session = DBSession() ShowTableData(session.query(Users).all(), 'users') def ShowTableData(resultSet, tablename): columns = ReflectTableColumns(Engine, Base.metadata, tablename) columnNames = list(columns.keys()) print (type(resultSet)) for row in resultSet: print (row.items[columnNames[1]]) print (row.columns[columnNames[1]]) print (row[columnNames[1]]) GetUsers() -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/f11bf60e-e872-489e-9a9b-03998440bbb1n%40googlegroups.com.