发送时,我在检索有关电影的信息时遇到问题
GET
向omdbapi.com的请求。
以下是处理对我的数据库的POST
请求的代码:
const express = require('express');
const router = express.Router();
const mongoose = require('mongoose');
const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
const Movie = require('../models/movie');
router.post('/', (req, res, next) => {
var temp;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if (this.readyState == 4 && this.status == 200) {
temp = JSON.parse(this.responseText);
}
};
xhr.open("GET", "http://www.omdbapi.com/?t=" + req.body.title + "&apikey=xyz", true);
xhr.send();
const movie = new Movie({
_id: new mongoose.Types.ObjectId(),
title: req.body.title,
movieInfo: temp,
comment: req.body.comment
});
movie
.save()
.then(result => {
console.log(result);
res.status(201).json({
message: 'Created movie succesfully',
createdMovie: {
_id: movie._id,
title: movie.title,
movieInfo: movie.movieInfo,
comment: movie.comment
}
});
})
.catch(err => {
console.log(err);
res.status(500).json({
error : err
});
});
});
module.exports = router;
我的问题是,除了movieInfo之外,其他所有东西都被传递了,我想成为一个嵌套的JSON对象,例如
createdMovie{
...
movieInfo: {
Title: a,
Actors: b,
...
...
}
...
}
这是我使用的猫鼬模式
const mongoose = require('mongoose');
const movieSchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
title: String,
movieInfo: String,
comment: String
});
module.exports = mongoose.model('Movie', movieSchema);
当我POST
看电影时,这就是测试时得到的(我使用Postman进行测试)。
{
"message": "Created movie succesfully",
"createdMovie": {
"_id": "5b7b0da48f5fb04e3c0f740b",
"title": "Goodfellas",
"comment": "some comment"
}
}
因此我缺少了movieInfo
,我不知道这是否是因为我以错误的方式传递了它,或者甚至没有显示它都没有到达该对象。尝试过使用不同的猫鼬类型,例如String
,Array
,Mixed
,但均无效。
如果请求成功,您获得的JSON就像这样
{
"Title":"Goodfellas",
"Year":"1990",
"Rated":"R",
"Released":"21 Sep 1990",
"Runtime":"146 min",
"Genre":"Crime, Drama",
"Director":"Martin Scorsese",
"Writer":"Nicholas Pileggi (book), Nicholas Pileggi (screenplay), Martin Scorsese (screenplay)",
"Actors":"Robert De Niro, Ray Liotta, Joe Pesci, Lorraine Bracco",
"Plot":"The story of Henry Hill and his life in the mob, covering his relationship with his wife Karen Hill and his mob partners Jimmy Conway and Tommy DeVito in the Italian-American crime syndicate.","Language":"English, Italian","Country":"USA","Awards":"Won 1 Oscar. Another 43 wins & 37 nominations.",
"Poster":"https://m.media-amazon.com/images/M/MV5BY2NkZjEzMDgtN2RjYy00YzM1LWI4ZmQtMjIwYjFjNmI3ZGEwXkEyXkFqcGdeQXVyNzkwMjQ5NzM@._V1_SX300.jpg",
"Ratings":[{"Source":"Internet Movie Database","Value":"8.7/10"},{"Source":"Rotten Tomatoes","Value":"96%"},{"Source":"Metacritic","Value":"89/100"}],
"Metascore":"89",
"imdbRating":"8.7",
"imdbVotes":"855,144",
"imdbID":"tt0099685",
"Type":"movie",
"DVD":"26 Mar 1997",
"BoxOffice":"N/A",
"Production":"Warner Bros.",
"Website":"N/A",
"Response":"True"
}
我想把它放在movieInfo
下。如何实现?
答案 0 :(得分:0)
请求是否以Content-Type发送-application / json?
答案 1 :(得分:0)
将movieInfo数据类型更改为“对象”。
const movieSchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
title: String,
movieInfo: Object,
comment: String
});
答案 2 :(得分:0)
一切都必须移到xhr.onreadystatechange
内部,因为如果没有,那么到我创建Movie
对象时,omdbapi.com的答案仍然没有到达。
xhr.onreadystatechange = function(){
if(this.readyState == 4 && this.status == 200){
*your code here*
}
}