Why can't I append an object in an array in Rethinkdb? - rethinkdb

I am trying to append an object into an array in rethink. Here is how I am trying to append it:
rethink('shifts')
.get(shiftId)
.update(row => row("milestones").default([]).append({
dateAchieved: date,
phaseType: phasetype.toUpperCase()
})).run(rethinkConnection)
The error I get is this:
UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): ReqlQueryLogicError: Expected type TABLE but found DATUM:
"shifts" in:
r("shifts").get("5d1b607f-e670-4eb5-b873-6e800f8ae8f8").update(function(var_15) { return var_15("milestones").default([]).append({"dateAchieved": r.ISO8601("2012-04-12T06:00:00.000Z"), "phaseType": "PILOT"}); })
^^^^^^^^
What does it mean that it is expecting a 'TABLE', but found a 'DATUM'? How do I get this to insert an object into the array?

You need to do
rethink.table(“shifts”)
Calling the rethink namespace with a string just returns an expression

Related

How to pass list to preparedQuery

How to pass list of ids to query.
List<Long> ids;
...
client.preparedQuery("SELECT id, name FROM features WHERE id IN $1").execute(Tuple.of(ids))
When I passed this query an error occurred saying invalid syntax near IN
You have to use the ANY operator:
client.preparedQuery("SELECT id, name FROM fruits WHERE id = ANY($1)")
.execute(Tuple.of(ids.toArray(new Long[ids.size()])));
With IN operator one has to specify each parameter, with the ANY you can pass an array.

Type error with Hasura array data type: "A string is expected for type : _int4"

I have a table I created in the Hasura console. A few of the columns are integer int types and I get the expected data type: Maybe<Scalars['Int']>.
However, a few needed to be an array of integers so I created those in the Hasura SQL tab:
ALTER TABLE my_table
add my_ids integer[];
If I populate those in GraphiQL with the following query variable everything works just fine:
{
"my_ids": "{1200, 1201, 1202}",
}
However, when I try to make the same request from my front-end client, I received the following error: A string is expected for type : _int4. Looking at the datatype, it is slightly different than the preset (in the data type dropdown) integer types: Maybe<Scalars['_int4']>.
Is there a way to get the array of integers to be Maybe<Scalars['Int']> like the preset integer types? Or if there isn't, how can resolve the issue with my request throwing errors for this _int4 type?
Hasura cannot process array but it can process array literals.
I've written a function to help you to transform your array into array literal before mutation:
const toArrayLiteral = (arr) => (JSON.stringify(arr).replace('[', '{').replace(']', '}'))
...
myObject.array = toArrayLiteral(myObject.array) // will make ['friend'] to {'friend'}
Good luck

Cannot Grab Data From Primary Key In RethinkDB

I'm attempting to grab data in RethinkDB by the primary key. However, my code cannot find the data.
I'm using the RethinkDBDash driver to grab the data and such, I defined Rethink as client.db. The ID is a sliced array from data I'm grabbing from an API, when I console log it, the value is 1. I tested this out by simply entering the integer 1 and it was able to grab the data, however, when I use the data from the API to grab it, it returns a null erorr. The table I am trying to grab data on is named suggestions with a primary key of sid, originally id. The table looks like this:
{
"author": {
"id": "535986058991501323"
} ,
"sid": 1 ,
"status": "PENDING" ,
"suggestion": "wtf"
}
On past projects, I have been able to grab data from the primarykey no problem using this exact method.
const id = args.slice(0).join(' ');
console.log(id);
const data = await client.db.table('suggestions').get(id).run();
console.log(data.sid);
From grabbing the data, I expected a console log with the value 1. However, what I got was an error that says this:
(node:34984) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'sid' of null
at Object.run (C:\Users\facto\Desktop\Projects\JavaScript\Bots\CountSill\commands\deny.js:7:26)
(node:34984) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:34984) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
I figured out the issue, the primarykey expects an integer rather than a string, so it looks for an integer rather than a string and since it can't find it, it throws an erorr. To fix it, I just had to change the data from an integer to a string. You can also just use the parseInt function to convert the array from a string to an integer.

Fatal error: Cannot use object of type stdClass as array in function array_push

I want to collect multiple checkbox values to process, but I got this
error = Fatal error: Cannot use object of type stdClass as array in this
line = foreach ($keluhan as $gejala) {
array_push($cf_array, $this->Maturan->get_nilai_cf($gejala, $penyakit['kode_penyakit']));
}
Is $panyakit an actual array that your trying to access or an object?
If it is an object try accessing it like this:
$penyakit->kode_penyakit

Error when retrieving and updating data from database in Codeigniter

In my application controller, I have a method transaction_reimburse() that receives ids as POST string values. The method's function is to get all transactions that match the ids from the database, and update them accordingly.
Controller method:
public function transaction_reimburse() {
$reimburse = array('reimburse' => 1);
$transactions = $this->Transaction_model->get_these_ids($this->input->post('ids'));
$this->Transaction_model->reimburse_these($transactions, $reimburse);
}
Model method:
function get_these_ids($ids) {
$this->db->select('tbl_user.name as uname, tbl_transactions.participant_id as pid, tbl_transactions.card_number as card_no, tbl_transactions.group as ugroup, tbl_toilet.name as utoilet, tbl_transactions.amount_paid as u_amount, tbl_transactions.toilet_price as t_price, tbl_transactions.reimburse_amount as r_amount, tbl_transactions.details as u_details');
$this->db->from('tbl_transactions');
$this->db->join('tbl_user', 'tbl_user.participant_id = tbl_transactions.participant_id', 'left');
$this->db->join('tbl_toilet', 'tbl_toilet.toilet_code = tbl_transactions.toilet_code', 'left');
$this->db->where("tbl_transactions.id IN (". $ids .")");
return $this->db->get();
}
Model method:
function reimburse_these($transactions, $reimburse) {
$this->db->select('tbl_user.name as uname, tbl_transactions.participant_id as pid, tbl_transactions.card_number as card_no, tbl_transactions.group as ugroup, tbl_toilet.name as utoilet, tbl_transactions.amount_paid as u_amount, tbl_transactions.toilet_price as t_price, tbl_transactions.reimburse_amount as r_amount, tbl_transactions.details as u_details');
$this->db->from('tbl_transactions');
$this->db->join('tbl_user', 'tbl_user.participant_id = tbl_transactions.participant_id', 'left');
$this->db->join('tbl_toilet', 'tbl_toilet.toilet_code = tbl_transactions.toilet_code', 'left');
$this->db->where("tbl_transactions.id IN (". $transactions .")");
$this->db->update($this->tbl_transaction, $reimburse);
}
However, I am getting the following error:
A PHP Error was encountered
Severity: 4096
Message: Object of class CI_DB_mysql_result could not be converted to string
Filename: models/transaction_model.php
Line Number: 450
A Database Error Occurred
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
UPDATE `tbl_transactions` SET `reimburse` = 1 WHERE `tbl_transactions`.`id` IN ()
Filename: C:\xampp\htdocs\ipa\system\database\DB_driver.php
Line Number: 330
What I need to know is why do I get these errors?
The errors mean that your $ids variable is not in a string format (meaning: 1,2,3,4. Its probably an array). To fix it, first find out what format it is on by using print_r($ids) or var_dump($ids). Once you know the format, use PHP functions(such as implode()) to transform it into strings.
IN is looking for a set in the format e.g. (1,2,3,4).
Either make your post values adopt the format 1,2,3,4 so that when the value is included into the query it's valid or if you're sending an array use a method in PHP such as implode(',',$array) to create the desired format for binding into the query..
I have reviewed your both function and found that in both function you put tbl_transactions.id in where clause and ids is passed from post and hence there is no need to call get_these_ids to get ids, you already have ids in post array and hence instead of transactions variable, just use $this->input->post('ids') for reimburse_these functions.
if you surely want to get tbl_transactions from get_these_ids function then you need to use group_concat with group by in query of get_these_ids function and just return that concated id in get_these_ids function then it will work.

Resources