Can't get the data from Object - react-native

I am confused with the data.
AsyncStorage.getItem('deviceuuid', (err, result) => {
console.log('result =>', result);
console.log('result.rawData =>', result.rawData);
})
console.log will show
Why result.rawData is undefined ?

Try JSON.parse(result.rawData)

That's because you are getting an String. AsyncStorage.getItem() returns an String and is likely that that string were a json before.
To resolve this, parse the result to a json.
const resultJSON = JSON.parse(result);
console.log('result.rawData =>', resultJSON.rawData);

The console shows that the data is in JSON format. But you can get the data without Parse.
let rawdata = await AsyncStorage.getItem ( 'deviceuuid');
console.log (rawdata);

Related

How to fix assertion error using playwright?

I'm using playwright to do e2e testing to check if the email is exists or not, and while I run my tests its said :
"Finished test flow with status passed"
But its show me that the test is failed because i have assertion error and not because i do something wrong
this is a piece my code:
const fillEmail = async (page: Page, value: string) =>
await page.fill("email-for-test", value);
const fillPassword = async (page: Page, value: string) =>
await page.fill("password-for-test", value);
await fillName(page, name);
await fillEmail(page, email);
const res = await page.locator('.email-error').innerText(); // return error string
expect(res).toContain("Email is already in used");
and i got error, you can see in the picture that I uploaded
any idea how to remove or fix this error
You code actually works. You can also use page.textContent()
const res = await page.locator('.email-error').innerText();
expect(res).toContain("Email is already in use");
const text = await page.textContent('.email-error');
expect(text).toContain("Email is already in use");
How about you just use this:
await expect(page.locator('.email-error')).toContainText(
'Email is already in used',
{timeout: 7000}
)
Also Check if the last word of the assertion message is used or use.
You can also use the text selector and assert it to be visible. Something like this:
await expect(page.locator('text=Email is already in use')).toBeVisible({
timeout: 7000,
})
i soulve this one by upgrade playwright library,
From v1.15 to v1.25

Array issue with Vuejs and axios

Hi I receive an answer from an axios like this:
axios.post('/api/hr_employee/days/'+ this.period_data.year +'/'+ this.period_data.month +'?page='+this.currentPage+'&api_token='+App.apiToken)
.then(response => {
this.posts = JSON.stringify(response.data.data);
console.log(this.posts.rut);
this.rowsQuantity = JSON.stringify(response.data.data.rut.length);
})
If I check the response it displays:
console.log(JSON.stringify(response.data.data));
{"rut":["06152617-K","06628494-8","06802969-4","06974036-7","07066149-7","07174172-9","07274753-4","07835227-2","08068401-0","08142157-9","08602937-5","08820315-1","08883533-6","09134412-2","09360615-9","09426000-0","09482390-0","09535406-8","09874855-5","10033721-5","10033741-K","10137545-5","10313233-9","10431151-2","10626085-0","10628514-4","10642899-9","10725575-3","10750262-9","10790603-7","10923452-4","10963260-0","11099111-8","11155155-3","11166398-K","11243121-7","11656483-1","11670463-3","11694645-9","11756501-7","11813180-0","11831400-K","11849551-9","11938713-2","12025353-0","12069398-0","12233298-5","12252924-K","12297111-2","12501219-1","12642557-0","12791259-9","12793546-7","12885196-8","12921934-3","12996111-2","13042688-3","13175573-2","13252171-9","13405296-1","13492547-7","13708510-0","13764500-9","13917718-5","14003387-1","14008227-9","14313469-5","14352987-8","14481738-9","14594420-1","15050542-9","15081798-6","15538007-1","15583027-1","15757306-3","15808735-9","15910256-4","15918421-8","15947022-9","16281232-7","16404869-1","16411890-8","16543379-3","16698947-7","16727358-0","16787383-9","16788913-1","16849126-3","17113779-9","17125113-3","17128461-9","17200607-8","17258553-1","17292825-0","17390237-9","17707004-1","17927553-8","17995199-1","18202568-2","18239456-4","18255994-6","18267465-6","18273096-3","18291733-8","18566961-0","18579236-6","18657051-0","18805028-k","18842465-1","18987839-7","19068615-9","19181860-1","19208732-5","19229525-4","19355444-K","19390553-6","19640455-4","19677914-0","19691447-1","19844084-1","19903195-3","19966543-K","20057484-2","20060059-2","20226059-4","20227575-3","20260330-0","20406658-2","20483426-1","20729074-2","20800385-2","20998161-0","21828453-1","21902443-6","22588139-1","22845770-1","23468753-0","23645227-1","23881022-1","24623559-7","24658304-8","24773648-4","24811238-7","24959860-7","25215942-8","25259612-7","25310541-0","25310683-2","25310734-0","25383726-8","25486922-8","25705970-7","25939855-K","26057740-9","26072855-5","26173938-0","26299443-0","26303301-9","26380591-7","26488988-k","26567665-0","26597593-3","26598819-9","26803446-3","26868737-8","26913967-6","26980959-0","27008182-7","27008183-5","27029453-7","27141399-8","27231254-0","27474935-0"],"full_name":["","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],"total_days":[30,30,30,21,30,30,30,30,30,30,30,30,30,30,27,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,1,30,30,30,26,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,17,30,30,30,30,30,30,30,30,30,30,29,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,26,30,30,30,30,1,30,30,16,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30]}
BUT if I do this
this.posts = JSON.stringify(response.data.data);
console.log(this.posts.rut);
undefined
so I wonder why does it display undefined if this.posts has same value? how can I fix it?
Thanks
I think it is because you stringify the response.data.data so this.posts data type become string. The string don't have rut property. That cause undefined when you log it.
I guess you don't need to JSON.stringify the response.data.data
try
this.posts = response.data.data;
this.rowsQuantity = response.data.data.rut.length;
That's because response from Axios is already in JSON format, but you are converting it to a string. To fix this issue, change the following line
From this:
this.posts = JSON.stringify(response.data.data);
To this:
this.posts = response.data.data;
You shall then be able to access the properties within the this.posts object
that's because you're trying to use the property access operator in a String value. You could fix that by calling JSON.parse(response.data.data) but I believe that axios already parses the response for you, so I think you can avoid using JSON.stringify.
JSON.stringify() returns a String value. You're getting undefined and not an error because Javascript does something called Object Wrapping (in this case it does new String(...))

AsyncStorage.getItem("item") returns the string "[object Object]"

I'm using AsyncStorage in order to save a "session" in my app, and I do so in this piece of code:
if (signInResult) {
if (signInResult.userId && signInResult.accessToken) {
await AsyncStorage.setItem("auth", signInResult);
}
this.props.navigation.navigate("home");
}
But when I tried to get the data back with AsyncStorage.getItem("auth"), it just retrieves the string [object Object]
I tweaked the code a but to write stuff in the console so I could keep tabs on the data, so here's what I encountered so far:
So there I logged the same data before and after putting it "through" the AsyncStorage. First, we see that the object is still very much a javascript object, and the second is just the name o the type, apparently. I'm not sure what's going on here, but I'm pretty sure this should work. I actually use that same approach in another app and I don't have this issue there.
Thanks in advance.
You need to JSON.stringify & JSON.parse while saving and getting data from AsyncStorage.
if (signInResult) {
if (signInResult.userId && signInResult.accessToken) {
await AsyncStorage.setItem("auth", JSON.stringify(signInResult));
}
AsyncStorage.getItem("auth").((item) => {
JSON.parse(item);
});
this.props.navigation.navigate("home");
}

AsyncStorage mergeItem - Unrecognised selector

Hi I'm trying to do a simple get and update using AsyncStorage in react native.
I get the data in componentDidMount() using the following code:
AsyncStorage.getItem('mytrips', (err, result) => {
if(result !== null){
var data = JSON.parse(result);
console.log(result);
this.setState({
email: data.email,
firstname: data.firstname,
lastname: data.lastname,
phone: data.phone,
mobile: data.mobile,
phonecode: data.phonecode,
DOB: moment(data.DOB).format('DD MMM YYYY'),
});
}
});
Following some user input I want to update what's in storage using mergeItem.
AsyncStorage.mergeItem('mytrips', JSON.stringify(data));
data is an object of all the changed values.
I get the following error when mergeItem is called.
ExceptionsManager.js:71 Exception '-[__NSArrayI objectForKeyedSubscript:]: unrecognized selector sent to instance 0x600000886400' was thrown while invoking multiMerge on target AsyncLocalStorage with params (
(
(
mytrips,
"[\"Carl\",\"Carl\",\"11111111\",\"111111111\",\"\",\"1900-01-01\",\"carl#test.com\"]"
)
),
3657
)
Any idea what could be causing the selector to be unrecognised? I'm not calling removeItem anywhere in my code.
You are merging an array of items as I can see. You can't merge an already stored JSON item with the array of items. As stated in the react-native docs:
MergeItem: Merges an existing key value with an input value, assuming both values are stringified JSON. Returns a Promise object.
Please try and merge the JSON object with the already saved item to work properly. I hope this solves your problem and if not please let me know :)

Component method response object data binding

I am starting to lose my mind in debugging an application that I inherited from a fellow developer who is absent.
I have narrowed down the problem to the following place in code (php files are checked, Vue instances are initialised, there are no syntax errors).
This is my the component that gets initialised:
var RadniStol = Vue.component('radnistol', {
template: '#template-RadniStol',
data() {
return {
tableData: [],
requestData: {
sort: "ID",
order: "DESC"
}
}
},
methods: {
reloadTable: function (event) {
data = this.requestData;
this.$http.post('php/get/radni_stol.php', data).then(response => {
console.log(response.data.bodyText);
this.tableData = response.data.records;
});
},
.
.
.
The PHP file that gets called with the POST method is working correctly, querying the database and echoing the response in a JSON format.
The thing that is making me pull out my hair is the following: the console.log(response.data) outputs the following into the console:
{"records":[{"DODAN_NA_RADNI_STOL":"1","..."}]}
It is an JSON object that I expected to have but when trying to assign it to the data of the component with:
this.tableData = response.data;
or any other way… response.data.records returns ‘undefined’ in the console. I have tryed with JSON.parse() but no success.
When logging types to console:
response variable is a response object with a status 200 and body and bodyText containing the data from the database.
response.data is a string type containing the string JSON with the data from the database.
When trying to use JSON.parse(response.data) or JSON.parse() on anything in the callback of the POST method I get the following error in the console:
RadniStol.js?version=0.1.1:17 Uncaught (in promise) SyntaxError: Unexpected token in JSON at position 0
at JSON.parse (<anonymous>)
at VueComponent.$http.post.then.response (RadniStol.js?version=0.1.1:17)
at <anonymous>
I am really starting to lose my mind over this issue, please help!
Thank you
If response.data is string, with JSON inside, then to access the records field, you should decode it like this:
JSON.parse(response.data).records
Not sure this has something to do with PHP or Vue.js, it is just plain javascript issue.
If it not decodes, than problem is definitely in response.data. For example
{"records":[{"DODAN_NA_RADNI_STOL":"1","..."}]}
is not a valid JSON, because key "..." needs to have some value.
But it seems to me that response.data is already parsed.
What I suggest you to do, is to write handler of the response as separate function, make response object that mimics actual response object by hand, and then test it separately from request. So you could show us request object and function that works with it.
I had the same error and fixed it.
Result will be response.body not response.data.
Here is my code:
getS: function(page) {
this.$http.get('vue-manager?page=' + page).then((response) => {
var data = JSON.parse(response.body);
this.student = data.data.data;
this.pagination = data.pagination;
});
},