我正在创建简单的rest API我有一个post / data端点,用于从外部API将数据保存到MongoDB,
这是我到目前为止所做的:
app.post('/data', (req, res) => {
let url = 'https://api.themoviedb.org/3/movie/401478/reviews?api_key=4d9c9de3bdf0d3b6837c49c086e3b190';
request(url, function (error, response, body) {
db.collection('data').insert(body, (err, result) => {
if (err) {
res.send({
'error': 'An error has occured'
});
} else {
res.send(result.ops[0]);
}
});
});
});
当我在控制台的邮递员localhost:8000/data
中测试api时,我收到错误:
TypeError:无法创建属性' _id'在字符串
我在这里做错了什么?
答案 0 :(得分:1)
body
是JSON字符串,要将其转换为JSON对象,请使用JSON.parse(body)
app.post('/data', (req, res) => {
let url = 'https://api.themoviedb.org/3/movie/401478/reviews?api_key=4d9c9de3bdf0d3b6837c49c086e3b190';
request(url, function (error, response, body) {
// convert to JSON
var bodyJson = JSON.parse(body)
db.collection('data').insert(bodyJson, (err, result) => {
if (err) {
res.send({
'error': 'An error has occured'
});
} else {
res.send(result.ops[0]);
}
});
});
});
答案 1 :(得分:0)
根据您之前的问题,我看到您的身体变量目前是一个字符串,因此您需要更改它。
app.post('/data', (req, res) => {
let url = 'https://api.themoviedb.org/3/movie/401478/reviews?api_key=4d9c9de3bdf0d3b6837c49c086e3b190';
request(url, function (error, response, body) {
var insertObj = JSON.parse(body)
db.collection('data').insert(insertObj, (err, result) => {
if (err) {
res.send({
'error': 'An error has occured'
});
} else {
res.send(result.ops[0]);
}
});
});
});
答案 2 :(得分:-1)
如您所见,“_ id不能是字符串”
当您请求提交“https://api.themoviedb.org/3/movie/401478/reviews?api_key=4d9c9de3bdf0d3b6837c49c086e3b190”
时它将返回看起来像JSON
的字符串'{"id":401478,"page":1,"results":[{"author":"Gimly","content":"There is some decent fun to be found in _Beyond Anarchy_. It's more _Escape from LA_ than it is _Death Race 2000_, but still an entry in the franchise, which brings me to the core problem of Beyond Anarchy: Is it even really a _Death Race_ movie? The answer is yes, but to go beyond that an ask: Should it have been a _Death Race_ movie? The answer's probably no.\r\n\r\nAs I said to begin with, I had some fun with the movie, but the things that kept bringing it down were its awkward, half-hearted attatchments to the movies in the series that had gone before it. If they had have abandoned those sentiments completely, it probably would have made a better viewing experience, but then, if that had been the case, how could you call it _Death Race 4_? The opposite approach probably would have worked too, having Beyond Anarchy be an actual sequel that follows _Death Race 3_ and what came before in a way that makes sense, but then, it couldn't have been even close to the movie that we got.\r\n\r\nInstead we have _Beyond Anarchy's_ sequel-limbo status, a movie that I don't regret watching, but that also can't really work for people who are fans of the _Death Race_ franchise, or for people who have never even seen a Death Race movie.\r\n\r\n_Final rating:★★½ - Had a lot that appealed to me, didn’t quite work as a whole._","id":"5af426b50e0a2639430091df","url":"https://www.themoviedb.org/review/5af426b50e0a2639430091df"}],"total_pages":1,"total_results":1}'
因此你必须使用JSON.parse使主体从一个字符串更改为另一个对象(将数据插入mongo必须是对象)
let url = 'https://api.themoviedb.org/3/movie/401478/reviews?api_key=4d9c9de3bdf0d3b6837c49c086e3b190';
request(url, function (error, response, body) {
body = JSON.parse(body);
db.collection('data').insert(body, (err, result) => {
//
});
});