【问题标题】:TypeError: Cannot create property '_id' on stringTypeError:无法在字符串上创建属性“_id”
【发布时间】:2018-11-24 15:00:03
【问题描述】:

我正在创建简单的 REST API 我有一个用于发布/数据的端点,用于将数据从外部 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”

我在这里做错了什么?

【问题讨论】:

  • body 不是从错误中明显看出的对象。
  • 您是否在npm 中添加了request 模块?
  • @AdamAzad 那么我需要对这个东西进行哪些更改?
  • @Alex 是的,其他端点有效

标签: javascript node.js mongodb express mongoose


【解决方案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]);
            }
        });
    });
});

【讨论】:

【解决方案2】:

根据您之前的问题,我看到您的 body var 当前是一个字符串,因此您需要更改它。

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]);
        }
    });
});
});

【讨论】:

    【解决方案3】:

    如你所见,“_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 以使 body 从字符串更改为对象(插入数据到 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) => {
            //
        });
    });
    

    【讨论】:

      猜你喜欢
      • 2023-04-11
      • 1970-01-01
      • 2022-01-25
      • 2019-07-20
      • 1970-01-01
      • 2019-02-27
      • 2018-12-08
      • 1970-01-01
      • 2017-09-22
      相关资源
      最近更新 更多