我正在使用Node MongoDB和Express。我从API调用接收JSON并尝试将其传递给我的数据库。它正在向数据库发送,但任何嵌套数组都会丢失它们的结构,并转变成一个像......这样的结构。
"nutritionEstimates[0][attribute]" : "FAT_KCAL",
"nutritionEstimates[0][description]" : "",
"nutritionEstimates[0][value]" : "310",
"nutritionEstimates[0][unit][id]" : "fea252f8-9888-4365-b005-e2c63ed3a776",
"nutritionEstimates[0][unit][name]" : "calorie",
"nutritionEstimates[0][unit][abbreviation]" : "kcal",
"nutritionEstimates[0][unit][plural]" : "calories",
"nutritionEstimates[0][unit][pluralAbbreviation]" : "kcal",
"nutritionEstimates[0][unit][decimal]" : "true",
"nutritionEstimates[1][attribute]" : "CAFFN",
"nutritionEstimates[1][description]" : "Caffeine",
"nutritionEstimates[1][value]" : "0.03",
"nutritionEstimates[1][unit][id]" : "12485d26-6e69-102c-9a8a-0030485841f8",
"nutritionEstimates[1][unit][name]" : "gram",
"nutritionEstimates[1][unit][abbreviation]" : "g",
"nutritionEstimates[1][unit][plural]" : "grams",
"nutritionEstimates[1][unit][pluralAbbreviation]" : "grams",
"nutritionEstimates[1][unit][decimal]" : "true",
我不知道具体是什么造成这种情况,但我怀疑也许它是和尚试图格式化JSON,如果我尝试将其作为字符串传递,它将不会传递数据。我的一些代码可供参考。已删除应用ID和密钥。
function saveRecipe(event){
event.preventDefault();
var recipeUrl = 'http://api.yummly.com/v1/api/recipe/' + this.value + '?_app_id=&_app_key=';
var json = $.getJSON(recipeUrl, function (data){
console.log(JSON.stringify(data));
$.ajax({
type: 'POST',
data: data,
url: '/saverecipe',
dataType: 'JSON'
});
});
};
我app.js的开头
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/favorite-recipes')
var routes = require('./routes/index');
var app = express();
我感谢任何人都能提供的任何见解。
答案 0 :(得分:0)
原来这是一个bodyParser选项“扩展”。它被设置为false并且不允许复杂对象。删除此选项可解决所有问题。