symfony - 键为“0,1,2,3,4,5,6”的数组的键 "id" 不存在
symfony - key "id" for array with key "0,1,2,3,4,5,6" does not exist
我的 symfony 项目有一个错误:
The key "id" for the array with the keys "0, 1, 2, 3, 4, 5, 6" does not exist.
我的数据库中不再有从 0 到 6 的对象,因为我不得不用 make:auth 覆盖数据,我相信是为了安全
我有新值,但我不知道如何避免此错误。
我试图让我的数据库中的攀登站点出现在我的 openstreet 地图上。因此,我使用传单 API.
循环访问 javascript 中的 ID
var sites = {
"cul de l'elephant": {
lat: 48.3727,
lon: 2.51059,
},
Paris: {
lat: 48.852969,
lon: 2.349903,
},
Brest: {
lat: 48.383,
lon: -4.5,
},
Quimper: {
lat: 48.0,
lon: -4.1,
},
Bayonne: {
lat: 43.5,
lon: -1.467,
},
};
var carte = L.map("macarte").setView([48.852969, 2.349903], 5);
L.tileLayer("https://{s}.tile.openstreetmap.fr/osmfr/{z}/{x}/{y}.png", {
attribution:
'données © <a href="//osm.org/copyright">OpenStreetMap</a>/ODbL - rendu <a href="//openstreetmap.fr">OSM France</a>',
minZoom: 1,
maxZoom: 20,
}).addTo(carte);
var icone = L.icon({
iconUrl: "../img/pointeur-de-carte.png",
iconSize: [50, 50],
iconAnchor: [25, 50],
popupAnchor: [-2, -44],
});
for (site in sites) {
var marqueur = L.marker([sites[site].lat, sites[site].lon], {
icon: icone,
}).addTo(carte);
marqueur.bindPopup(
<a href="{{ path('site_show', {'id':sites.id}) }}">site</a>
);
}
我收到上面引用的错误。
这是我的数据库的结构
谢谢。亲切
看看 documentation for for...in
,当你使用 sites.id
时它没有指向任何东西。
我假设数据与您显示的不同,并且您确实在元素上具有 ID 值。如果那是真的,并且您的数据看起来像代码段中的那样,则您必须将该引用更新为 sites[site].id
。否则,编辑您的答案以显示您的数据的样子,上面有 ID。
var sites = {
"cul de l'elephant": {
id: 12,
lat: 48.3727,
lon: 2.51059,
},
Paris: {
id: 5,
lat: 48.852969,
lon: 2.349903,
},
Brest: {
id: 34,
lat: 48.383,
lon: -4.5,
},
Quimper: {
id: 75,
lat: 48.0,
lon: -4.1,
},
Bayonne: {
id: 8,
lat: 43.5,
lon: -1.467,
},
};
for (site in sites) {
console.log(`Site "${site}" lat is ${sites[site].lat} and lng is ${sites[site].lon}`);
// If your sites had an ID property, this is how you would access it
console.log(`Site "${site}"'s ID is ${sites[site].id}`);
}
console.log('####################');
// You could make the code easier to read updating the variable names
for (siteName in sites) {
const site = sites[siteName];
console.log(`Site ${siteName}: Id: ${site.id}, lat: ${site.lat}, lng: ${site.lon}`);
}
听起来你控制了服务器,一个改进可能是将方法更新为return一个站点数组,而不是一个以站点名称作为键的对象,如下所示:
const sites = [
{
id: 12,
name: "cul de l'elephant",
lat: 48.3727,
lon: 2.51059,
},
{
id: 5,
name: 'Paris',
lat: 48.852969,
lon: 2.349903,
},
{
id: 34,
name: 'Brest',
lat: 48.383,
lon: -4.5,
},
{
id: 75,
name: 'Quimper',
lat: 48.0,
lon: -4.1,
},
{
id: 8,
name: 'Bayonne',
lat: 43.5,
lon: -1.467,
},
];
sites.forEach( site => {
console.log(`Site ${site.name}: Id: ${site.id}, lat: ${site.lat}, lng: ${site.lon}`);
});
我的 symfony 项目有一个错误:
The key "id" for the array with the keys "0, 1, 2, 3, 4, 5, 6" does not exist.
我的数据库中不再有从 0 到 6 的对象,因为我不得不用 make:auth 覆盖数据,我相信是为了安全
我有新值,但我不知道如何避免此错误。
我试图让我的数据库中的攀登站点出现在我的 openstreet 地图上。因此,我使用传单 API.
循环访问 javascript 中的 IDvar sites = {
"cul de l'elephant": {
lat: 48.3727,
lon: 2.51059,
},
Paris: {
lat: 48.852969,
lon: 2.349903,
},
Brest: {
lat: 48.383,
lon: -4.5,
},
Quimper: {
lat: 48.0,
lon: -4.1,
},
Bayonne: {
lat: 43.5,
lon: -1.467,
},
};
var carte = L.map("macarte").setView([48.852969, 2.349903], 5);
L.tileLayer("https://{s}.tile.openstreetmap.fr/osmfr/{z}/{x}/{y}.png", {
attribution:
'données © <a href="//osm.org/copyright">OpenStreetMap</a>/ODbL - rendu <a href="//openstreetmap.fr">OSM France</a>',
minZoom: 1,
maxZoom: 20,
}).addTo(carte);
var icone = L.icon({
iconUrl: "../img/pointeur-de-carte.png",
iconSize: [50, 50],
iconAnchor: [25, 50],
popupAnchor: [-2, -44],
});
for (site in sites) {
var marqueur = L.marker([sites[site].lat, sites[site].lon], {
icon: icone,
}).addTo(carte);
marqueur.bindPopup(
<a href="{{ path('site_show', {'id':sites.id}) }}">site</a>
);
}
我收到上面引用的错误。
这是我的数据库的结构
谢谢。亲切
看看 documentation for for...in
,当你使用 sites.id
时它没有指向任何东西。
我假设数据与您显示的不同,并且您确实在元素上具有 ID 值。如果那是真的,并且您的数据看起来像代码段中的那样,则您必须将该引用更新为 sites[site].id
。否则,编辑您的答案以显示您的数据的样子,上面有 ID。
var sites = {
"cul de l'elephant": {
id: 12,
lat: 48.3727,
lon: 2.51059,
},
Paris: {
id: 5,
lat: 48.852969,
lon: 2.349903,
},
Brest: {
id: 34,
lat: 48.383,
lon: -4.5,
},
Quimper: {
id: 75,
lat: 48.0,
lon: -4.1,
},
Bayonne: {
id: 8,
lat: 43.5,
lon: -1.467,
},
};
for (site in sites) {
console.log(`Site "${site}" lat is ${sites[site].lat} and lng is ${sites[site].lon}`);
// If your sites had an ID property, this is how you would access it
console.log(`Site "${site}"'s ID is ${sites[site].id}`);
}
console.log('####################');
// You could make the code easier to read updating the variable names
for (siteName in sites) {
const site = sites[siteName];
console.log(`Site ${siteName}: Id: ${site.id}, lat: ${site.lat}, lng: ${site.lon}`);
}
听起来你控制了服务器,一个改进可能是将方法更新为return一个站点数组,而不是一个以站点名称作为键的对象,如下所示:
const sites = [
{
id: 12,
name: "cul de l'elephant",
lat: 48.3727,
lon: 2.51059,
},
{
id: 5,
name: 'Paris',
lat: 48.852969,
lon: 2.349903,
},
{
id: 34,
name: 'Brest',
lat: 48.383,
lon: -4.5,
},
{
id: 75,
name: 'Quimper',
lat: 48.0,
lon: -4.1,
},
{
id: 8,
name: 'Bayonne',
lat: 43.5,
lon: -1.467,
},
];
sites.forEach( site => {
console.log(`Site ${site.name}: Id: ${site.id}, lat: ${site.lat}, lng: ${site.lon}`);
});