【发布时间】:2017-03-20 09:17:43
【问题描述】:
我正在从 Amazon API 检索有关书籍的数据并将其存储为 JSON 对象。然后我想将此数据插入到 Meteor 集合中。但是,JSON 数据中的某些字段以“$”字符开头,导致出现此错误:
MongoError: 'result.results.0.ItemAttributes.0.ManufacturerMaximumAge.0.$' 中的美元 ($) 前缀字段 '$' 对存储无效。
鉴于数据包含“$”符号,我希望帮助将数据插入集合中。如果这不可能,有没有办法删除 JSON 数据中所有出现的“$”,然后将其存储到 Meteor 集合中? 这是我的代码,给定书名和作者,它搜索 Amazon API 并返回有关前 10 个结果的信息:(SearchResult 是对集合的引用)
Meteor.methods({
itemSearch(appUUID,title, author) {
var result = {};
result.search = {
appUUID: appUUID,
title: title,
author: author
}
client.itemSearch({
title: title,
author: author,
searchIndex: 'Books',
responseGroup: 'ItemAttributes, Images'
}).then(function(results){
result.results = results;
var upsertResult = SearchResult.upsert( {
appUUID: appUUID,
title: title,
author: author,
},
{
$set: {result: result}
}
);
}).catch(function(err){
SearchResult.upsert({
appUUID: appUUID,
title: title,
author: author,
},
{
$set: {result: result }
}
);
});
}
});
这是 JSON 数据,“单位”之前的美元符号是导致问题的原因。
"Width": [
{
"_": "810",
"$": {
"Units": "hundredths-inches"
}
}
]
【问题讨论】:
标签: javascript mongodb amazon-web-services meteor