Getting an error, text.match error when no results exist - actionscript-3

I have code that looks like this
if (consearch.text.match(/kau$/).length >= 0)
{
trace("test");
}
which works fine if consearch.text value ends with "kau"
unfortunately when it does not end with "kau" I get a 1009 error
Am I missing something?? Not sure why its giving me this error

If you dont know how to use packages then use it like this, this is part of the code link #DodgerThud suggests,
function endsWith(input:String, suffix:String):Boolean {
return (suffix == input.substring(input.length - suffix.length));
}
if (endsWith(consearch.text,"kau"))
{
trace("test");
}

I know that you got an answer, but I'll try to explain why you got that error ?
So you got that error because String.match() returns an array ONLY if there is at least one substring in the string (consearch.text) that matches the specified pattern (/kau$/) otherwise it's returning null, and that's why that error is fired when your text didn't contain kau at its end.
If you want always to use RegExp, you can do it like this for example :
if (/kau$/.test(consearch.text))
{
trace('test');
}
Hope that can help.

Related

Conditional inside es6 .map

project im working on used es6/jsx and the airbnb linter but im having some trouble with the following code: i need to map items and compare it to another id but when i write it like this i get an error on the if statement, which is "parsing error unexpected token".
tried parentheses around the item, brackets after fat arrow, but not sure what the issue is. dont need to add return since it knows to expect it back. trying to find the way to get this working with the correct syntax.
const cartItems = items.map(item => {
if (id === item._id) {
console.log('found', item_id);
}
});
edit:
doing it like that, the .map(item has an error: expected parentheses around arrow function having curly braces.
moving over the => { i get an error: expected to return a value in arrow function
and in the console.log the item_id has a error: item._id is not defined, it should have been defined with the map but seems its not seeing it?
basically need to loop through the items id's and match them against another set of ids, if they match i need to combine the matching ones into a new variable
Ive changed it a bit, for what i need by doing it like this:
if (id === items.map(item => item._id)) {
this.setState({ inCart: true });
}
then just use the state value to conditionally load anything i needed.

How to use Transactions in cakephp

I write this code, but its not working, it gives this error
Call to a member function commit() on a non-object
Hear is my code
$datasource = $this->Arrear->getDataSource();
$datasource->begin();
if($this->Customar->saveField("total_bake",$amount) && $this->Arrear->save()){
$dataSource->commit();
return $this->redirect(array('action'=>'index'));
}else{
$dataSource->rollback();
$this->Session->setFlash('Data insert Failed','failure');
}
Variables in php(and hence in cakephp as well) are case-sensitive
http://php.net/manual/en/language.variables.basics.php
you have in your first line
$datasource = $this->Arrear->getDataSource();
but you are committing like
$dataSource->commit();
you have the data source assigned to $datasource, but not to $dataSource. The last variable even is not defined, that is why it is showing that error. So, you have to be sure you are using exactly the same variable (with same capitalization) in all places.

Attempt to perform arithmetic on field 'x' (a table value)

I'm working with lua-alchemy, and I'm setting a global variable in my AS3 code in this way:
_lua.setGlobal("map", _map);
With _map being a object with the following function in it:
public function get x():int
{
return 10;
}
if then I try to do something like this in Lua
local a = map.x + 1
I get the following error:
Lua script failed: luaDoString:21: attempt to perform arithmetic on field 'x' (a table value)
Does anyone knows why it does that, and how I could fix it?
EDIT :
When I print type(map.id), it prints table... Shouldn't it print number?
I found my error. According to this page, I have to use as3.tolua(map.x) to convert it to the right type.

How do I return JSON from an action in CFWheels?

I've been reading a lot about returning JSON in CFWheels...it all looks straight forward, but all I ever get is a blank page with no JSON returned. I may be doing something really obviously wrong here.
Here's my action:
public void function ajax() {
param name="params.keyword" default="";
onlyProvides("json");
pins = model("pin").findAll(
include = "user",
order = "createdat DESC",
where = "title LIKE '%#cleanInput(application.jsoup, params.keyword)#%'"
);
renderWith(data=pins, layout=false);
}
I can confirm that searching on a word and dumping the query result reveals a record, but when it's like above, all I get is a blank screen. Am I missing something here?
I want to return the query object in JSON. Is there something I have to actually output in my view? I could do this, but wante to do it all from the action as I thought it would?
Thanks,
Mikey.
Just off the top of my head are you passing Format=JSON in your ajax call? If not the controller will not return JSON.

AS3: Null object reference when attempting to check if it exists?

I have a weird problem. An Object is being passed to my function, and some parameters are optional, so naturally I would check to see if they are there, and if not, do nothing.
However, I'm getting a null reference error (#1009) when I'm just checking it. Here's the sample:
public function parseObject(params:Object) {
if (params.optionalParam)
trace("Got Optional Parameter!");
}
The error is returned on the line with the if statement. Changing it to check for null (if (params.optionalParam == null)) doesn't work either. The players seems to just give up if an object doesn't exist.
Is there any logical reason for this to happen? Or is it some weird bug that has just surfaced?
Thanks in Advance,
-Esa
If your params object is null, then you will get a null reference error when trying to access its' optionalParam property.
Try something like:
if (params == null)
{ trace("params is null!"); }
else if (params.optionalParam != null)
{ trace("Got optional parameter!"); }