Dataset Query not reading all fields in database

I understand this may be the stupid question of the week, but I can’t seem to make this work. When performing a database query, I cannot get all the fields to show up in the Record. I am using the standard procedure:

$w("#dataset1").onReady( () => {
let count = $w("#dataset1").getTotalCount();

 wixData.query("DailyBetList")
  .find()
  .then( (results) => {
 let allItems = results.items; 

allItems.forEach((record) => {

etc. I’ve done it for every other field in the database several times. This leads to an update code, but when inspecting what the record contains after the find - results combination, I do not find the field I wish to update. Originally, it was set up as a ‘number’ field, and the Update function gave me this error:

Error: Invalid update. Updated object must have a string _id property.

I checked the database and changed the original field from ‘number’ to ‘text’, but the error persisted. So I removed that field and created a new one as text from scratch and the error persists. All fields are in ‘text’ format now.

here’s the complete code I am working on:

export function UpdateRowNumbers_click(event) {
 //Add your code for this event here: 
$w("#dataset1").onReady( () => {
let count = $w("#dataset1").getTotalCount();
let rowNumber = 0;

 wixData.query("DailyBetList")
  .find()
  .then( (results) => {
 let allItems = results.items; 

allItems.forEach((record) => {
 

 if (rowNumber === 0 || rowNumber < count){
  rowNumber = rowNumber+ 1;
 var rowNumber1 = rowNumber.toLocaleString();
  }

allItems.hiddenRowCount = rowNumber1;

console.log("this is rownumber", allItems.hiddenRowCount);

 ///updating database with results
 let Id = allItems._id;
 let b_leagueTournament = allItems.leagueTournament;
 let b_RecommendedBet = allItems.recommendedBet;
 let b_playerTeamOne = allItems.playerTeamOne;
 let b_playerTeamTwo = allItems.playerTeamTwo;
 let b_recommendedBetOdds = allItems.recommendedBetOdds;
 let b_createdDate = allItems._createdDate;
 let b_HFR = allItems.hiddenFinalResults;
 let b_finalResults = allItems.finalResults;
 let b_profitLoss = allItems.profitLoss;
 let b_sumProfitLoss = allItems.sumProfitLoss;
 let b_rowNumber = allItems.hiddenRowCount;

  console.log(record);

 let toUpdate = {
 "_id": Id,
 "leagueTournament": b_leagueTournament,
 "recommendedBet": b_RecommendedBet,
 "playerTeamOne": b_playerTeamOne,
 "playerTeamTwo": b_playerTeamTwo,
 "recommendedBetOdds": b_recommendedBetOdds,
 "hiddenFinalResults": b_HFR,
 "_createdDate":b_createdDate,
 "finalResults":b_finalResults,
 "profitLoss":b_profitLoss,
 "sumProfitLoss":b_sumProfitLoss,
 "hiddenRowCount":b_rowNumber
     };

updateBet(toUpdate);

});

  } )
  .catch( (err) => {
 let errorMsg = err;
  } );

});
}

the field ‘hiddenRowCount’ does not appear in the record when the query is run, so I think there is something that is making the darn thing be invisible to the query, but I have no idea what that might be. No errors appear at the line level, so it seems that it all looks good, but the field does not show up on the record, these are the fields and the test data that show up when the query is run:

_id: 
"9f058c9c-11da-4dd7-99af-8d92f220ac88"
leagueTournament: 
"another test"
recommendedBet: 
"another one"
playerTeamOne: 
"another one"
playerTeamTwo: 
"another two"
recommendedBetOdds: 
"1.3"
hiddenFinalResults: 
"1"
_createdDate: 
"2018-11-23T09:00:44.286Z"
finalResults: 
"Win"
profitLoss: 
"0.30"
sumProfitLoss: 
"1.29"
_owner: 
"b898fb7f-6157-41b4-a709-ec24fed911fc"
_updatedDate: 
"2018-11-27T12:54:35.898Z"

Suggestions on reasons why the field does not appear in the record will be deeply appreciated so I can test them out.

Thank you.

Hello

Some reasons for this might be:

  1. You forgot to add the field, in this case in the database it would look like [FIELD NAME] with the brackets. All youd have to do here is just click on it in the database and press add to collection.

  2. You have it hidden, to unhide it :


3. Make sure you are taking the field KEY, not the field label in the database. Click on the field that is not showing up in your collection and click on edit field, then make sure it is exactly how you have it in the code section

If these 3 dont work, post your sites url here and I will take a look

Goodluck,
Majd

I checked the visibility in the database and the field was set up to visible.
I use the key information hiddenRowCount (“hiddenRowCount” from the DB).
I have a table on the page that was showing the ‘old’ field, so I deleted that column and added the correct column with the updated field in it.
Republished the site after checking on all three items.

sorry, no joy on the code performing differently. same issue as before.

site is called aibetzy.com.

Let me know if there is something else I can try, thank you ahead of time.

Hey there!
I have experienced the same thing as you when the fields value is null it seem that it is not included at all in the results. Can you check if the field is empty?

field is empty, but that is about to change. I will put in a test value and see if it makes a difference.

sorry, no joy on that one. added “0” to all the fields in the test database and still get the same error. My table on the page updated no problem.

Sounded like a good idea at the time. How did you solve the problem in your case?

Andreas was correct, adding something to the field in the database solved the problem of it not appearing as part of the record when doing a query. However, I still get the same error message:

Error: Invalid update. Updated object must have a string _id property.

which I cannot resolve. I am not sure where the error is in the code, or if there is something else that is supposed to be done to get it to work.

this happens even though the missing field is now listed, like this:

any suggestions on how to interpret the error message so I can make appropriate corrections?

_id: 
"a1d268ff-84e4-4ffc-aa77-8ac63c090b23"
leagueTournament: 
"test 3"
recommendedBet: 
"more"
playerTeamOne: 
"more"
playerTeamTwo: 
"moreone"
recommendedBetOdds: 
"3.5"
hiddenFinalResults: 
"1"
_createdDate: 
"2018-11-01T09:33:15.700Z"
finalResults: 
"Win"
profitLoss: 
"2.5"
sumProfitLoss: 
"1.29"
_owner: 
"b898fb7f-6157-41b4-a709-ec24fed911fc"
_updatedDate: 
"2018-11-27T14:50:13.866Z"
hiddenRowCount: 
"0"

Majd, my DB does not show the option to have the index field visible, is there a reason for that?

I added the 0s to the field in the test database and now the field does show up in the query. Go figure. However, I still get this message:

Error: Invalid update. Updated object must have a string _id property.

I know this may be weird, but it may be that the query is going over the number of records available and giving an error on that?

The code does seem to run the way it is supposed to. Any ideas will be appreciated.

@daniel20660 Hey
Yes it is so, empty fields will not be returned by wixData.query. If you try to update a record with a parameter called _id that is not of type string you will get that error. Try to look into your updateBet function or paste it here so we can look at it.

@andreas-kviby

This is the code I use to load the _id of the record into a variable, together with everything else that is on the specific record:

///updating database with results
 let Id = allItems._id;
 let b_leagueTournament = allItems.leagueTournament;
 let b_RecommendedBet = allItems.recommendedBet;
 let b_playerTeamOne = allItems.playerTeamOne;
 let b_playerTeamTwo = allItems.playerTeamTwo;
 let b_recommendedBetOdds = allItems.recommendedBetOdds;
 let b_createdDate = allItems._createdDate;
 let b_HFR = allItems.hiddenFinalResults;
 let b_finalResults = allItems.finalResults;
 let b_profitLoss = allItems.profitLoss;
 let b_sumProfitLoss = allItems.sumProfitLoss;
 let b_rowNumber = allItems.hiddenRowCount;

 //console.log(record);
if (Id !== null || Id !== undefined){
 let toUpdate = {
 "_id": Id,
 "leagueTournament": b_leagueTournament,
 "recommendedBet": b_RecommendedBet,
 "playerTeamOne": b_playerTeamOne,
 "playerTeamTwo": b_playerTeamTwo,
 "recommendedBetOdds": b_recommendedBetOdds,
 "hiddenFinalResults": b_HFR,
 "_createdDate":b_createdDate,
 "finalResults":b_finalResults,
 "profitLoss":b_profitLoss,
 "sumProfitLoss":b_sumProfitLoss,
 "hiddenRowCount":b_rowNumber
     };

       console.log("this is toupdate", toUpdate);

this is what the ‘console.log’ statement reports on what is inside the ‘toUpdate’ variable:

this is toupdate 


{...}

hiddenRowCount: 
"1"

db_admin3
Line 627
this is toupdate 


{...}

hiddenRowCount: 
"2"

db_admin3
Line 627
this is toupdate 


{...}

hiddenRowCount: 
"3"

db_admin3
Line 627
this is toupdate 


{...}

hiddenRowCount: 
"4"

db_admin3
Line 627
this is toupdate 


{...}

hiddenRowCount: 
"5"

db_admin3
Line 627
this is toupdate 


{...}

hiddenRowCount: 
"6"

db_admin3
Line 627
this is toupdate 


{...}

hiddenRowCount: 
"7"

db_admin3
Line 627
this is toupdate 


{...}

hiddenRowCount: 
"8"

db_admin3
Line 627
this is toupdate 


{...}

hiddenRowCount: 
"9"

db_admin3
Line 627
Error: Invalid update. Updated object must have a string _id property.



Error: Invalid update. Updated object must have a string _id property.



Error: Invalid update. Updated object must have a string _id property.



Error: Invalid update. Updated object must have a string _id property.



Error: Invalid update. Updated object must have a string _id property.



Error: Invalid update. Updated object must have a string _id property.



Error: Invalid update. Updated object must have a string _id property.



Error: Invalid update. Updated object must have a string _id property.



Error: Invalid update. Updated object must have a string _id property.

it looks like only the ‘hiddenRowCount’ variable is loading into the toUpdate variable even though all the others are supposed to be in there…or am I reading this incorrectly? Just to be safe, I put a not null or not undefined control in an if statement in front of the definition of toUpdate, thinking that that might shield me from bad _id coming through. didn’t work, though.

@daniel20660 Hey, try this

console.log("this is toupdate", JSON.stringify(toUpdate));

@andreas-kviby

At the risk of sounding profoundly ignorant…did you want me to create a different variable and load into it the Json.stringfy result, then send that variable through the update function? Not sure if that will work since the json.stringify may not be a JS object once it is done, correct? Or am I missing something of genius? Once applied, the result in the console.log is the same as before…

would you mind to clarify a bit what to do next?

Andreas: I figured it out. You were right about the method for getting the field to show up in the query, and I had to use record.field name as opposed to allitems.fieldname to tell the system where to get the data for updating for the toUpdate function.

It’s working fine now, as it is supposed to. It was going to take me 20 minutes this morning, but I have spent most of the day puzzling this one out. Like I said, stupid question of the day.

Thank you for your patience.

Hello Daniel,

My guess is that you have empty rows in your database and as such they have no _id fields, this causes an error in the update function because the _id parameter is required. Try going to the collection, deleting all the empty rows, then syncing with live version. Then before the update function and setting up the toUpdate object, console.log the id so you see if there is one for each update.

Goodluck,
Majd

it is working properly now but I will most certainly take this into account next time this kind of weirdness shows up. thanks.