简体   繁体   中英

TypeError: Cannot create property '_id' on string

I am creating simple rest API I have an endpoint for post/data to save data to the MongoDB from external API,

Here is what I have so far:

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

when i test api in postman localhost:8000/data in console I get error:

TypeError: Cannot create property '_id' on string

What am I doing wrong here?

body is the JSON string, to convert it to JSON object, use 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]);
            }
        });
    });
});

Base on your previous question, I see your body var is currently a string, so you need to change it.

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

As you can see, " _id can't be string "

when you request to url " https://api.themoviedb.org/3/movie/401478/reviews?api_key=4d9c9de3bdf0d3b6837c49c086e3b190 "

it will return string that look like 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}'

so you must do the JSON.parse to make the body change from string to object (inserting data to mongo must be object)

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) => {
        //
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM