我有一个在后端端的地址的json文件。从客户端,我通过API获取用户的地址作为post方法。 我想检查用户输入的地址是否已经存在于JSON文件中。 在json文件中的地址已经是小写的合成。 我是Node和JS的新手。 这是我试过的。 和我得到错误“消息”:“address.equals不是一个函数”
const addressesAsJson = require('../data/addresslist.json') function addressCheck ( address /* : string | void */ ) /* :Promise<Object[]> */ { var convertAdLowerCase = address.toLowerCase() return Promise.resolve() .then(() => { console.log(address) return addressesAsJson.filter((address) => address.equals(convertAdLowerCase)) console.log(address.equals(convertAdLowerCase)) }) }我的JSON格式看起来像这样
{"address":"new south park , western sydney , australia 2345","display":"New South Park , WESTERN SYDNEY , AUSTRALIA 2345"}I have a json file with addresses in backend side.. From client side I'm getting an address of user as a post method through API. I want to check whether the address entered by user is already exist in JSON file or not. In json file address are already as lowercase formate. I'm tottally new to Node and JS. This is what I tried. and I'm getting error "message": "address.equals is not a function"
const addressesAsJson = require('../data/addresslist.json') function addressCheck ( address /* : string | void */ ) /* :Promise<Object[]> */ { var convertAdLowerCase = address.toLowerCase() return Promise.resolve() .then(() => { console.log(address) return addressesAsJson.filter((address) => address.equals(convertAdLowerCase)) console.log(address.equals(convertAdLowerCase)) }) }My JSON format look like this
{"address":"new south park , western sydney , australia 2345","display":"New South Park , WESTERN SYDNEY , AUSTRALIA 2345"}最满意答案
使用相同的运算符
address === convertAdLowerCase并删除返回后的控制台日志,因为它是无法访问的代码。
完美的代码将是
return Promise.resolve() .then(() => { console.log(address); const allMatches = addressesAsJson.filter((record) => record.address === convertAdLowerCase)); console.log(allMatches); return allMatches.length > 0; });Use equal operator
address === convertAdLowerCaseand remove the console log which is after the return since it is unreachable code.
Perfect code would be
return Promise.resolve() .then(() => { console.log(address); const allMatches = addressesAsJson.filter((record) => record.address === convertAdLowerCase)); console.log(allMatches); return allMatches.length > 0; });更多推荐
发布评论