ParseJSON - Not Working - Forum

Forum Navigation
You need to log in to create posts and topics.

ParseJSON - Not Working

Page 1 of 2Next

Hi..

I connect into MySQL database with the following

//Select Users
$sqlAlias[1]="SelectUsers";
$sqlQuerys[1]="SELECT * FROM Users";
$sqlMaxUserLevel[1]=-1;

i use in a button the following

neoPhpExecSql "DB" "SelectUsers" "" "TakeDataFromDatabase"

My subroutine has a string named DataFromDatabase and inside the subroutine i use the following

CreateEmptyObject [userinfo]
ParseJSON "[DataFromDatabase]" [userinfo]

The problem is that i don't get any result back from the JSON command. The connection in remote database is working because all my other SQL queries are working.

Header is on, on config.php

header("Access-Control-Allow-Origin: *");

My app is not running from (Run from Start) and in Firefox console i see multiple errors. Also i don't have any idea where i can examine the variables and the returning neoPhpExecSql result variables.

Any ideas ?

@smartmedia add this line at the very top of TakeDataFromDatabase subroutine.

ConsoleLog "[DataFromDatabase]"
Then execute your app and use F12 to see the Web Browser Console.
Any error message or data coming from neoPhp plugin will be displayed there.
Please, let me know what you see.
Regards.

I open the console from Microsoft Edge cause Firefox it shows earlier version of compiled app even after flush temp.

I get:

Uncaught SyntaxError: Unexpected token 'o', "[object Obj"... is not valid JSON
    at JSON.parse (<anonymous>)
    at $scope.TakeDataFromDatabase (main.js:35:22)
    at Object.success (main.js:166:809)
    at c (jquery.min.js:2:28327)
    at Object.fireWith [as resolveWith] (jquery.min.js:2:29072)
    at l (jquery.min.js:2:79901)
    at XMLHttpRequest.<anonymous> (jquery.min.js:2:82355)

 

@smartmedia please add this line at the VERY TOP:

ConsoleLog "[DataFromDatabase]"

Before trying to convert [DataFromDatabase] to JSON.
I mean, before using ParseJSON.

cause Firefox it shows earlier version of compiled app even after flush temp.

Try reloading the app by pressing Ctrl-F5

smartmedia has reacted to this post.
smartmedia

Lui,

I put it inside the subroutine on top as u said.

In the image shows that it gets the results but i don't get it in variables for some reason.

[userinfo.id]
[userinfo.name]
[userinfo.username]
[userinfo.passwordr]
[userinfo.email]
[userinfo.accounttype]
[userinfo.registerdate]
[userinfo.error]

 

@smartmedia I think the JSON is already parsed. Please use this instead of your original code:

CreateEmptyObject [userinfo]
SetVar [userinfo] [DataFromDatabase]

Regards.

 

Lui,

When i use only the

CreateEmptyObject [userinfo]
SetVar [userinfo] [DataFromDatabase]

the variable [userinfo] has

 [{"id":"1","name":null,"username":"admin","password":"123456","email":"smartmediagr@gmail.com","accounttype":null,"registerdate":null},{"id":"2","name":null,"username":"test","password":"test","email":"test@test.gr","accounttype":null,"registerdate":null},{"id":"3","name":null,"username":"niki","password":"123456","email":"test@test1.gr","accounttype":null,"registerdate":null},{"id":"5","name":null,"username":"nikos","password":"123456","email":"test@test1.gr","accounttype":null,"registerdate":null},{"id":"6","name":null,"username":"niki","password":"123456","email":"test@test1.gr","accounttype":null,"registerdate":null},{"id":"7","name":"niki","username":"niki","password":"123456","email":"test@test1.gr","accounttype":"Πελάτης","registerdate":"2022-11-24 20:00:20"},{"id":"8","name":null,"username":"niki","password":"123456","email":"test@test1.gr","accounttype":null,"registerdate":null}]

but the following variables are empty.

[userinfo.id]
[userinfo.name]
[userinfo.username]
[userinfo.passwordr]
[userinfo.email]
[userinfo.accounttype]
[userinfo.registerdate]
[userinfo.error]

Am i missing something here ?

@smartmedia you are working with an Array of data objects, not a single data object. Check this for the first item:

[userinfo(0).id]
[userinfo(0).name]
[userinfo(0).username]
[userinfo(0).passwordr]
[userinfo(0).email]
[userinfo(0).accounttype]
[userinfo(0).registerdate]
[userinfo(0).error]

Or this for the second one:

[userinfo(1).id]
[userinfo(1).name]
[userinfo(1).username]
[userinfo(1).passwordr]
[userinfo(1).email]
[userinfo(1).accounttype]
[userinfo(1).registerdate]
[userinfo(1).error]

Or this for the [n] item (starting at 0):

[userinfo([n]).id]
[userinfo([n]).name]
[userinfo([n]).username]
[userinfo([n]).passwordr]
[userinfo([n]).email]
[userinfo([n]).accounttype]
[userinfo([n]).registerdate]
[userinfo([n]).error]

Regards.

Lui,

All those variables are empty, do i need to create first an array ? Sorry but i have been rusty all those years away of programming. Excuse me if i bother you.

@smartmedia

Let me start with an explanation of what is happening/observed ...

1)

$sqlQuerys[1]="SELECT * FROM Users";

You are asking to fetch all fields (columns) for all records in the Users Table.

2) It is returning an array variable (as seen in the [ ... ])

Each array item is returned as an object (inside the { ... }), and is the content of records e.g. ...

{"id":"1","name":null,"username":"admin","password":"123456","email":"smartmediagr@gmail.com","accounttype":null,"registerdate":null}

Objects for each record are separated by commas.

Inside each record content, you have key/value pairs ... id, username, password etc. are the keys, and correspond with the names of fields (columns) in your Users table.

The values for each of these fields are the ones after the : ... e.g. "password":"123456" says that the password field's content (for that object/record) is 123456

The suggestion from @luishp is correct ... but I believe the reason you are getting nothing being returned is because, you mentioned in your first post that ...

My subroutine has a string named DataFromDatabase

... so, I believe that the returned data is being treated as a string (plain text) and not as an array of objects.

I do not have access to your server/database, so I am not able to verify this suggestion, which is to try and specify the Type for your parameter as ARRAYDATA instead of STRING.

 

I also suggest that you verify this part first by doing this before copying the received data to [userinfo] ...

SetVar "smartEmail" "[DataFromDatabase(0).email]"
AlertBox "smartEmail" "[smartEmail]" ""

... you should see the value of "smartmediagr@gmail.com"

Good Luck.

smartmedia has reacted to this post.
smartmedia

Hi Gaev,

Long time to hear you and always a pleasure to take your advice. I am searching a solution 5 days now, i followed your advice and put above setvar in order to check as you said. The script is not playing at all for some reason, no alertbox is shown and of course [DataFromDatabase(0).email] is empty . I did changed the type of subroutine into ArrayData and delivers again the same results but without the

SetVar "smartEmail" "[DataFromDatabase(0).email]"
AlertBox "smartEmail" "[smartEmail]" ""

If i leave only this line

SetVar [userinfo] [DataFromDatabase]

i got the results, so it seems for some reason the CreateEmptyObject [userinfo] not working. I wonder if it has something to do with installation or missing library. My version is the latest 22.10.17

I also created a button and passed the contains of the [userinfo] into [userinfo1] and create a new empty object but also is not working. i don't get any results.

I can give you access to my MySQL database if you want and have spear time to help. I really don't know what to do to overcome it.

 

@smartmedia I'm sure you are very close to solve the problem but it's really difficult to help you without having real access to your app.
Have you checked the YouTube tutorial for working with databases? Can you succesfully execute the same sample app (included with VisualNEO Web) within your environment?

I am about to propose that we use AnyDesk so that I can take a look directly on your computer in real time.

smartmedia has reacted to this post.
smartmedia

Lui,

I have check Utube videos and did exactly what is referring to. I get SQL results after all.

I can provide you remote connection any time you like. My facebook is

https://www.facebook.com/Tsouknidakos/

if you like to discuss it further.

@smartmedia

I followed your advice and put above setvar in order to check as you said. The script is not playing at all for some reason, no alertbox is shown and of course [DataFromDatabase(0).email] is empty .

Oops, I seem to have made another typo error in my last suggestion (blame it on senior moments).

I forgot to place brackets around the smartEmail (to indicate it is a variable) in the SetVar command; should have been ...

SetVar "[smartEmail]" "[DataFromDatabase(0).email]"
AlertBox "smartEmail" "[smartEmail]" ""

... try the above modification.

it seems for some reason the CreateEmptyObject [userinfo] not working

Whenever I get unexpected results, I break the whole process into baby steps; then I run the first step and verify that the results are as expected ... then, on to the next baby step etc.

So, the above suggested commands would be the only commands in the subroutine (for the first baby step).

I took a quick look at the code in SampleApps\neoPhpSamples\neoPhpSimplestDatabase.neoapp provided by @luishp ... note that ..

a) the received data is defined as a string (not an array as per my earlier suggestion)

b) the code in the subroutine looks like this ...

CreateEmptyArray [tableData]
SetVar [tableData] [data]
ArrayLen [tableData] [totalRecords]
SetVar [currentRecord] 0
SetVar [recordId] [tableData([currentRecord])('id')]
SetVar [recordPainting] [tableData([currentRecord])('painting')]
SetVar [recordAuthor] [tableData([currentRecord])('author')]etc.

... the first command is CreateEmptyArray (not CreateEmptyObject).

Hi Geav and Lui,

Thanks for your patience, i create a test neoapp only to test the specific code. What i have found is that if i have a variable structed like this

[userinfo(0).id]
[userinfo(0).name]

anywhere in the app, (spesific the variable is inside a headline object) the console give me multiple errors on mutliple js files. Check the image.  if i remove the (0) from the variable = [userinfo.id] i don't receive any errors in console. I don't know what is cause this behavior.

https://imgur.com/a/igd17M6

I don't know how to continue from this point over.

When i use only the

CreateEmptyObject [userinfo]
SetVar [userinfo] [DataFromDatabase]

the variable [userinfo] has

[{"id":"1","name":null,"username":"admin","password":"123456","email":"smartmediagr@gmail.com","accounttype":null,"registerdate":null},{"id":"2","name":null,"username":"test","password":"test","email":"test@test.gr","accounttype":null,"registerdate":null},{"id":"3","name":null,"username":"niki","password":"123456","email":"test@test1.gr","accounttype":null,"registerdate":null},{"id":"5","name":null,"username":"nikos","password":"123456","email":"test@test1.gr","accounttype":null,"registerdate":null},{"id":"6","name":null,"username":"niki","password":"123456","email":"test@test1.gr","accounttype":null,"registerdate":null},{"id":"7","name":"niki","username":"niki","password":"123456","email":"test@test1.gr","accounttype":"Πελάτης","registerdate":"2022-11-24 20:00:20"},{"id":"8","name":null,"username":"niki","password":"123456","email":"test@test1.gr","accounttype":null,"registerdate":null}]

@smartmedia please check the attached app.
Note that I have just copy-pasted the data you already have.

Regards.

Uploaded files:
  • You need to login to have access to uploads.
smartmedia has reacted to this post.
smartmedia

Lui,

I have checked your example and it runs like a charm. Although It does not take those data from the server. When i replace your

BeginJS
  $App.userinfo = [.........];
EndJS

with the variable who comes as result from neoPhpExecSql i still receive errors. I tried to save the contains of the variable into a text file to examine it and this is what i get in file.

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

I will send you in pm the credentials to mySQL database.

I really appreciate the time and effort u have put and believe me i want to find a solution in order to move on.

PS. I guess i cant send you a pm so i will use the contact form..

Problem finnaly solved and i like to thank Lui for he's time and effort.

It appears that when you have a variable constructed like this [userinfo(0).id] in a text or container object it cause the script to freeze and receive errors on execution.

I also was curious how to get the total number of records as callback from an SQL Query and Lui told me about the [userinfo.length] and that any JavaScript property can be accessed directly using dot notation (this is somehow advanced), but you can use ArrayLen instead if you prefer.

luishp and Vadim have reacted to this post.
luishpVadim
Quote from smartmedia on November 28, 2022, 10:08 pm

It appears that when you have a variable constructed like this [userinfo(0).id] in a text or container object it cause the script to freeze and receive errors on execution.

Hello, please tell me which way to solve this problem have you found? I think I have a similar situation, but only I don't use neoPHP

Page 1 of 2Next