我得到一个带有对象数组的对象输出。我完全与此映射混淆,我这样做时出错,所以我需要将其转换为normalizr。我不知道该怎么做。我的谢玛在下面给出
代码:-
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
require('mongoose-double')(mongoose);
var Schema = mongoose.Schema;
var SchemaTypes = mongoose.Schema.Types;
var TestSchema = new Schema(
{
name:{type: String, required: true},
type: {type: Boolean},
artists: {type: Array},
total_count: {type: String},
active: {type: Boolean}
}
);
module.exports = mongoose.model('Test', TestSchema);
我正在检索类似的数据:-
代码:-
router.post('/getoneartist',function(req, res){
Test.findOne({_id : req.body.id}).populate({ path: 'artists', model: 'Artist' }).exec()
.then(function(test){
res.send(test)
})
})
现在我正在获取这样的数据:
{
id:123,
name:new,
type:true,
artists:[
{id:123,name:artist1}
{id:126,name:artist2}
],
total_count:25,
active:true
}
在映射数据时出现未定义的错误。因此我需要将其转换为normalizr格式。有什么办法可以将其转换为另一种格式。
预先感谢
答案 0 :(得分:0)
看起来您的JSON 不正确
工作示例:
const {map} = require('lodash');
let test = {id:123, name:"new", type : true, artists: [{id:123,name:"artist1"}, {id:126,name:"artist2"} ],total_count:25,active:true}
let name = map(test.artists,'name'); //lodash
let name_native = test.artists.map(item=>{ return item.name});//native
console.log(name);
console.log(name_native)
反应中,您可以使用Map by
import { map} from 'lodash';
let name = map(test.artists,'name'); //from lodash
let name_native = test.artists.map(item=>{ return item.name}); //native