Builder content link
Builder public api key
257b1927f27c4d8c83f0f0c2db302e0a
What are you trying to accomplish
I need to access a single entry in a structured data list by querying for the url field that is nested inside a referenced page. The problem is the data I need is inside a nested object with the key “value”. That is a reserved word so the MongoDB style query is not allowing me to access the page data such as url and title.
Code stack you are integrating Builder with
NextJS
Reproducible code example
This works because of the “id” property:
const singleEntryTest = await builder.getAll('inspiration-traget-test-model', {
query: {
'data.targetcategoryPages': {
$elemMatch: {
'categoryPage.id': {
$regex: '257b1927f27c4d8c83f0f0c2db302e0a_5cecc1cc1b0d4269b0160ca15381118f'
}
}
}
}
});
This does not return any records and it shows how I cannot access the critical page data because the query cannot access things in a “value” property:
const singleEntryTestTwo = await builder.getAll('inspiration-traget-test-model', {
query: {
'data.targetcategoryPages': {
$elemMatch: {
'categoryPage.value': { $exists: true }
}
}
}
});
Hello,
I’ve listed below some possible solutions
Fetch Broader
Fetch a broad set of documents where you suspect your data might be.
1import { builder } from '@builder.io/sdk';
2
3// Initialize Builder SDK with your API key
4builder.init('257b1927f27c4d8c83f0f0c2db302e0a');
5
6const fetchBroadData = async () => {
7 try {
8 const response = await builder.getAll('inspiration-traget-test-model', {
9 limit: 100, // Fetch more if needed for your data volume
10 }).toPromise();
11 return response;
12 } catch (error) {
13 console.error('Error fetching data', error);
14 return [];
15 }
16};
Filter Client-Side
Filter the fetched data to get the specific entry based on the nested url
field.
1const filterDataByUrl = (data, urlToFind) => {
2 return data.filter(item => {
3 return item.data.targetcategoryPages.some(categoryPage => {
4 return categoryPage.categoryPage.value && categoryPage.categoryPage.value.url === urlToFind;
5 });
6 });
7};
8
9// Example Usage
10const findSpecificEntryByUrl = async (urlToFind) => {
11 const allData = await fetchBroadData();
12 const filteredResults = filterDataByUrl(allData, urlToFind);
13
14 if (filteredResults.length === 0) {
15 console.log('No matching records found');
16 } else {
17 console.log('Matching records found:', filteredResults);
18 }
19
20 return filteredResults;
21};
22
23// Call the function with the specific URL you are looking for
24const urlToFind = "https://example.com";
25
26findSpecificEntryByUrl(urlToFind).then(data => {
27 console.log(data);
28}).catch(err => {
29 console.error('Error finding specific entry', err);
30});
Please let me know if these suggestions point you in the right direction