Uncaught TypeError: Cannot read properties of undefined (reading 'image') when trying to sort api response data - sorting

I am trying to sort object using the sort method in my NextJs project. The api response data is as follows:
enter image description here
And my code to sort by latest and oldest is :
let sorted;
if (sortKey==="latest")
{
sorted = cspaces.attributes.image.data[0].attributes.sort((a,b) => b.createdAt - a.createdAt
)
cspaces = sorted
console.log(cspaces)
}
else if(sortKey==='oldest')
{
sorted = cspaces.attributes.image.data[0].attributes.sort((a,b) => a.createdAt - b.createdAt
)
cspaces = sorted;
console.log(cspaces)
}
But I am getting the following error : TypeError: Cannot read properties of undefined (reading 'image')
cspaces.attributes.image is not empty as you can see in the img, what am I doing wrong here?

Related

How to destructure form input values from useFormikContext?

I am trying to destructure values to get specific value of the form inputs using useFormikContext()
I have done like:
const { values } = useFormikContext()
const { name, age } = values
but I'm getting error like:
Property 'name' does not exist on type 'unknown'
Fixed it.
I'm using typescript, when I added the interface to the useFormikContext() the error was gone.
Did it like this: useFormikContext<ProfileFields>()

Parse Server relation.add() not working

Issue Description
If I create a relation and use the relation.add() method on it nothing happens.
Steps to reproduce
let Booking = Parse.Object.extend("Booking")
let bookingQuery = new Parse.Query(Booking)
bookingQuery.get(newBookingObject.id, {
success: booking => {
console.log("invites", invites)
// prints array of existing ParseObjects from another class
let relation = booking.relation("invites")
console.log(relation)
// prints new relation with key "invites" and parent "Booking"
relation.add(invites)
console.log(relation)
// prints exactly the same relation object as before - nothing changed
booking.save().then(res => console.log(res))
// an empty relation is added
},
error: error => console.log("error", error)
// no errors
})
Expected Results
relation.add(arrayOfParseObjects) should add objects to relation
Actual Outcome
Nothing happens
insted of
relation.add(invites)
please try the following
for (var i =0; i<invites.length;i++){
relation.add(invites[i])
}
Regards

sys_id arrays to is one of not displaying records on my report

I am not able to display records on my report.
Report Source: Group Approval(sysapproval_group) table
Condition:Sys Id - is one of - javascript: new GetMyGroupApprovals().getSysIds();
Script Include : MyGroupApproval
Note : Active is checked, Accesible is all application score & Client callable unchecked
var GetMyGroupApprovals = Class.create();
GetMyGroupApprovals.prototype = {
initialize: function() {
},
getSysIds : function getMyGroupMembers(){
var ga = new GlideRecord('sysapproval_group');
ga.addQuery('parent.sys_class_name', '=', 'change_request');
ga.query();
gs.log("TotalRecords1 Before:: " + ga.getRowCount());
var sysIdArray = [];
while(ga.next()){
sysIdArray.push(ga.sys_id);
}
return sysIdArray;
},
type: 'GetMyGroupApprovals'
};
Kindly note that I have to achieve with script approach. I am not able to get records on my report.
This line is probably causing unexpected behavior:
sysIdArray.push(ga.sys_id);
ga.sys_id returns a GlideElement object, which changes for each of the iterations in the GlideRecord, so the contents of sysIdArray will just be an instance of the same object for each row in the result set, but the value will just be the last row in the set.
You need to make sure you push a string to the array by using one of the following methods:
sysIdArray.push(ga.sys_id+''); // implicitly call toString
sysIdArray.push(ga.getValue('sys_id')); // return string value
Quick suggestion, you can use the following to get sys_ids as well:
sysIdArray.push(ga.getUniqueValue());

How can use json data bind in datatable and grivdview in mvc

I am parsing JSON Data in custom gridview and I get the following error:
I am using the Newtonsoft.Json.NET dll.
Additional information: Unexpected JSON token when reading DataTable. Expected StartArray, got StartObject. Path '', line 1, position 1.
I am using this code
var result = _profitGenDiagnosticsService.GetSQLQueryResult(profitGenQueryEditorModel.SqlQuery);
var o = JObject.Parse(result);
DataTable dt = JsonConvert.DeserializeObject<DataTable>(o.ToString());
profitGenQueryEditorModel.SqlQueryResult = dt;
return PartialView("_ProfitGenQueryResult", profitGenQueryEditorModel);

yii2 ActiveRecord findBySql - Response content must not be an array Error

New to Yii2 nuances.
Just trying to get a return from a ActiveRecord query. I realize there is probably a much easier way to do this using Yii2 conventions
public function actionGet_permissions() {
$sql = 'select * from auth_item where owner_user_id IS NULL';
return Auth_Item::findBySql($sql)->all();
}
Errors "Response content must not be an array."
I think its pretty obvious the simple set of records I'm trying to return with this function. Any help is much appreciated, and let me know if any other information will help.
Why would findBySql not be allowed to return an array? I know I'm missing something simple here.
Thanks!
Edit 1:
Auth_Item::find()->where(['owner_user_id' => null])->all();
Returns the same error. And again, this seems like such a simple query.
Edit 2:
Stack Trace:
Invalid Parameter – yii\base\InvalidParamException
Response content must not be an array.
• 1. in C:\xampp\htdocs\clienti\vendor\yiisoft\yii2\web\Response.php at line 944
throw new InvalidConfigException("The '{$this->format}' response formatter is invalid. It must implement the ResponseFormatterInterface.");
}
} elseif ($this->format === self::FORMAT_RAW) {
$this->content = $this->data;
} else {
throw new InvalidConfigException("Unsupported response format: {$this->format}");
}
if (is_array($this->content)) {
throw new InvalidParamException("Response content must not be an array.");
} elseif (is_object($this->content)) {
if (method_exists($this->content, '__toString')) {
$this->content = $this->content->__toString();
} else {
throw new InvalidParamException("Response content must be a string or an object implementing __toString().");
}
}
}
}
• 2. in C:\xampp\htdocs\cli\vendor\yiisoft\yii2\web\Response.php – yii\web\Response::prepare() at line 312
• 3. in C:\xampp\htdocs\cli\vendor\yiisoft\yii2\base\Application.php – yii\web\Response::send() at line 381
• 4. in C:\xampp\htdocs\cli\frontend\web\index.php – yii\base\Application::run() at line 18
Edit 3:
Thanks for the help guys. Json encoding the result fixed the issue.
public function actionGet_permissions() {
$result = Auth_Item::find()->where(['owner_user_id' => NULL])->all();
return Json::encode($result);
}
You should use Yii2 features and modify response format.
Default response format is HTML, that's why you have the following error :
Response content must not be an array
You should simply try this :
public function actionGet_permissions()
{
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
return Auth_Item::find()->where(['owner_user_id' => NULL])->all();
}
Yii will automatically send http headers (previous answer will not) and encode your models.
Read more : http://www.yiiframework.com/doc-2.0/guide-runtime-responses.html
And if you want to use this response format in all your controllers, you could modify response config :
'response' => [
'format' => yii\web\Response::FORMAT_JSON,
'charset' => 'UTF-8',
],
Read more about yii\web\Response
Use Active Record:
public function actionGet_permissions() {
$result = Auth_Item::find()->where(['owner_user_id' => NULL])->all();
return Json::encode($result);
}
Better You make changes directly into your configuration file so that every request gets the proper json encoding.
'response' => [
'format' => yii\web\Response::FORMAT_JSON,
'charset' => 'UTF-8',
],
format json makes the variable post process automatically scalar by converting to json, whereas html response type does not convert arrays to scalars, therefore arrays have to have json response, I accidentally changed response type to HTML, but did forget to get rid of array structure at a particular place

Resources