I am trying to fetch data from commerce.js by following documentation. After fetching data I am getting a object named Meta.
function fetchProducts() {
commerce.products.list()
.then((res) => {console.log("products Data",res);})
.catch((error) => {console.log('There was an error fetching the products', error);});
}
useEffect(()=>{
fetchProducts();
}, [])
output == products Data {meta: {…}}meta: {pagination: {…}}[[Prototype]]: Object
I don't know if it is working fine, or there are any mistakes from my side. As per documentation, It is an array of objects not meta.
Robbie from the Commerce.js team here. It looks as if no products are being returned in your account. Do you have any set up?
We have a known issue where the data key, which is normally an array of objects, doesn't get returned when no results are available.
Had the same issue fetching a specific product from commerce.js using a query parameter.
Turns out the query parameter value had to be all lower case. At least it made it worked in my case.
I hope this will help someone.
Related
I'm incredibly stuck on trying to format receive a JSON response, and format it. Order of operations:
Query a REST API endpoint (https://endpoint.com/api/v1/employee?id={username})
Receive JSON response: {"employee":{"full name":"Example Name","function":"Role","office":"Office Location",team":"Team 1|Team 2|Team 3|"}}
In my base.js file within my django app, I am hoping to extract the team strings and pass them into another URL. What way can I do this? When I $.getJSON from the endpoint I receive responseJSON, responseText, etc. but I'm unable to pull them out/use them in any way.
In order to fetch and parse json I would suggest you to use the following fetch structure:
fetch('https://endpoint.com/api/v1/employee?id=username')
.then(response => response.json())
.then(data => {
let teams = data.team.split('|');
// Do something with team names
});
Let's break down what this does per line:
First we use fetch to request data from the defined url
Then we convert the response to a json object
Finally we retrieve the string with team values using the data.teams statement, after which we immediately convert the list of team names to an array by using split and the defined delimiter |.
After this you should be able to do whatever you'd like with the team names. You could use them to make another API call as well. If you're interested, be sure to checkout the fetch documentation, as well as the split documentation if you are not yet familiar with that function.
The solution above assumes that the response will be a 200, for error handling you should check out the fetch documentation above.
I need to get data from collection based on Id but I'm unable to fetch details.
I have tried certain methods but none worked as expected.
Example 1
return this.db.collection("customers/", id).snapshotChanges();
It says to pass odd numbers of segment
My function
this.auth.getUserDetails(this.userid).subscribe(data => {
console.log(data);
});
Example 2
return this.db.collection('customers')
.doc(id)
.get();
What would be the better way to fetch and display data?
You can go like this:
// Assuming ID is passed as an argument.
this.db.doc(`costumers/${id}`).snapshotChanges();
The reason why you're getting problems is because you're using collection instead of doc.
If I query my users collection and filter by 2 different properties as seen below, it returns the expected data.
return this.afs
.collection<Employee>('users', ref => {
return ref
.where('accountId', '==', accountId)
.where('isEmployee', '==', true);
})
.valueChanges()
However, if I try to order that data by name, as seen below, it returns an empty array.
return this.afs
.collection<Employee>('users', ref => {
return ref
.where('accountId', '==', accountId)
.where('isEmployee', '==', true)
.orderBy('name');
})
.valueChanges()
I figured I must need to manage the way indexing is being handled or something, but did some research and my understanding is that if that were the case, I would get an error message in the console with a link to setup the composite indexing that I need. I don't get any errors though, just an empty array. What am I doing wrong? Is it possible to order a collection of data that is filtered by 2 properties? I believe I am using Firebase version 6.5, but am willing to update if that will solve my problem.
I would get an error message in the console with a link to setup the composite indexing that I need.
Yes, that's correct. When you are using a query like yours, a warning message should be displayed in the console.
I don't get any errors though, just an empty array.
In this case, you should create that index manually in the Firebase console.
Is it possible to order a collection of data that is filtered by 2 properties?
Sure it is. Just create the required index and then you should be able to execute the query.
It doesn’t look like you’re ready to catch errors. May want to do something like:
.catch(error => console.log(error)
(Will format that when on desktop)
I'm new to axios.
In the past when I've made http requests I'm used to getting back an array/array of objects and this allows me to easily format the data how I want by using functions such as map and reduce. I then would render it to the DOM.
I've noticed in the response I get back is an observer object. How would I go about making the request so it gives me back an array? What is the standard for dealing with this observer object?
getSomething (myId) {
return axios.get('/api/getSomething', {params: {'id': myId}})
.then(response => console.log(response.data))
.catch((promise) => this.handleError(promise));
}
Thanks
EDIT:
Updated code.
To clarify, when I call getSomething() response.data is an object even though I am sending it as an array on the backend. I am assuming that axios is changing this array to an object. The object has a bunch of extra properties like __ob__ and get 0
So I found the issue. If you pass through an array where the keys are not in order e.g. [1: [], 5: [], 6:[]]. Javascript will change it into a observer object which has different properties in order to maintain the keys. This issue is not related to axios.
You can do something as simple as the following to access the data:
axios.get('/some/url').then(response => {
console.log(response);
});
I was searching for an easy and simple database for a little highscore system for a some games I'm developing in javascript.
I saw Orchestrate.io in github's student developer pack. I found a suitable drivermodule nodejs orchestrate and have integrated them.
The problem comes with querying orchestrate for my data. I have managed saving scores and querying them with db.list('collection'), but this seems to not responding with all data. It appered to me that some values are not returned.
I read about the db.search('collection','query') function. But I don't really understand how I could return all data because I don't want to query in a specific way.
My objects are as simple as follows:
{"name":"Jack","score":1337}
As I understand, one has to send a key, when putting such values to an orchestrate-collection. But I'd like to query the whole collection and get the values in a descendant order in regard to the score.
As for now I end up sorting the result on the client-side.
I hope you guys can give me some hints for a query that can sort for specific values!
You have the option to use a SearchBuilder
db.newSearchBuilder() //Build a search object
.collection('collection') //Set the collection to be searched
.sort(score, 'desc') //Set the order of the results
.query("*") //Empty search
.then(function (res) { //Callback function for results
//Do something with the results
})
Source
By default, .list uses a pagination limit of 10. You can either increase that, e.g.:
db.list('collection', { limit: 100 })
Or use .links, .links.next (from the docs):
db.list('collection', { limit: 10 })
.then(function (page1) {
// Got First Page
if (page1.links && page1.links.next) {
page1.links.next.get().then(function (page2) {
// Got Second Page
})
}
})