[英]Node-Express: filter request query and display rest of the data
我是 Node 查詢的新手。 我正在為我的后端應用程序使用 Node express。 我有一個嵌套的 json,它有三種語言選項,內部語言有auth
、 dashboard
、 data
和data1
選項。 我想過濾查詢並在瀏覽器中顯示其余的 json 數據。 例如,如果我像這樣輸入 url: http://localhost:5000/?namespaces=auth&languages=en,fi
那么它將顯示語言en
和fi's
數據以及我想顯示auth's
數據的namespaces
。 為了顯示數據,我創建了一個輸出空對象,並希望將其添加到我的輸出對象中。 但不知道該怎么做。
我已經在codeandbox 中分享了我的代碼。
這是我的json數據
{
"en": {
"auth": {
"welcomeMessage3": "Hi John"
},
"dashboard": {
"welcomeMessage": "Hi Doe"
},
"data (1)": {
"welcomeMessage3": "Hi Jonny"
},
"data": {
"welcomeMessage3": "Hi Monty"
}
},
"fi": {
"auth": {
"welcomeMessage3": "Moi name "
},
"dashboard": {
"welcomeMessage": "Moi dashboard"
},
"data (1)": {
"welcomeMessage3": "Moi data 1"
},
"data": {
"welcomeMessage3": "Moi data"
}
},
"sv": {
"auth": {
"welcomeMessage3": "Hej John"
},
"dashboard": {
"welcomeMessage": "Hej dashboard"
},
"data (1)": {
"welcomeMessage3": "Hej data"
},
"data": {
"welcomeMessage3": "Hej data"
}
}
}
這是我的快遞應用
const express = require('express')
const app = express()
const port = 5000
const translationData = require('./translations'); // My json
const filterTranslations = (namespaces, languages) => {
let output = {};
const translations = { ...translationData };
console.log("I am typing Languages", languages);
console.log("I am typing namespace", namespaces);
for (const lng in translations) {
console.log("languages are coming from translation json", lng);
if (lng.length !== 0 && lng !== languages) {
delete translations[lng];
console.log("Delete rest of the language", lng);
} else {
for (const ns in translations[lng]) {
if (ns.length !== 0 && ns !== namespaces) {
delete translations[lng][ns];
console.log("delete rest of the Namespace", ns);
}
}
}
}
return output;
};
app.get('/', (req, res) => {
res.send(
filterTranslations(
req.query.namespaces,
req.query.languages,
)
)
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
有幾個錯誤:
filterTranslations
應該返回一個值,這里是translations
,if
語句中的條件不正確: lng !== languages
,在您的示例中, languages = 'en,fi'
和lng
將是en
或fi
。 查看String.includes( searchString [, position ])
或split
您的languages
並使用Array.includes( searchElement[, fromIndex])
。希望能幫到你,祝你有美好的一天!
如果我正確理解你想要做什么,我認為你把事情復雜化了。
首先,將逗號分隔的字符串作為參數傳遞並不是最好的主意,因為您必須通過在逗號中拆分字符串將其轉換為后端的數組。 最好是從數組開始。 這意味着您的網址將是:
http://localhost:8080/?namespaces=auth&languages[]=en&languages[]=fi
那么處理這個的函數將是:
const filterTranslations = (namespaces, languages) => {
let output = [];
const translations = { ...translationData };
//languages are now an array
for (let l in languages) {
let lng = languages[l];
// for each language you send as a parameter you push a new object in your output with the language as a key and the values
output.push({[lng]:translations[lng]});
}
return output;
};
預期輸出如下:
[
{
"en": {
"auth": {
"welcomeMessage3": "Hi John"
},
"dashboard": {
"welcomeMessage": "Hi Doe"
},
"data (1)": {
"welcomeMessage3": "Hi Jonny"
},
"data": {
"welcomeMessage3": "Hi Monty"
}
}
},
{
"fi": {
"auth": {
"welcomeMessage3": "Moi name "
},
"dashboard": {
"welcomeMessage": "Moi dashboard"
},
"data (1)": {
"welcomeMessage3": "Moi data 1"
},
"data": {
"welcomeMessage3": "Moi data"
}
}
}
]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.