我正在尝试将对象数组保存到mongoose模式但是无法将数据加载到array<struct<string, int>>
模式中我只看到一个包含laptop
的插入。
如何将json对象的数组保存到mongoose模式?
controller.js
id
schema.js
var laptopData = [
{ name: 'Hp probook 15.1',cpu:'2.4GHZ' ,ram: '4GB', harddrive: '160GB',Brand:'HP'},
{ name: 'Hp probook 15.1',cpu:'2.4GHZ' ,ram: '4GB', harddrive: '160GB',Brand:'Hp'},
{ name: 'Hp probook 15.1',cpu:'2.4GHZ' ,ram: '4GB', harddrive: '160GB',Brand:'Hp'},
{ name: 'Hp probook 15.1',cpu:'2.4GHZ' ,ram: '4GB', harddrive: '160GB',Brand:'Hp'},
{ name: 'Hp probook 15.1',cpu:'2.4GHZ' ,ram: '4GB', harddrive: '160GB',Brand:'Hp'},
{ name: 'Hp probook 15.1',cpu:'2.4GHZ' ,ram: '4GB', harddrive: '160GB',Brand:'Hp'},
{ name: 'Hp probook 15.1',cpu:'2.4GHZ' ,ram: '4GB', harddrive: '160GB',Brand:'HP'},
{ name: 'Hp probook 15.1',cpu:'2.4GHZ' ,ram: '4GB', harddrive: '160GB',Brand:'Hp'},
{ name: 'Hp probook 15.1',cpu:'2.4GHZ' ,ram: '4GB', harddrive: '160GB',Brand:'Hp'},
{ name: 'Hp probook 15.1',cpu:'2.4GHZ' ,ram: '4GB', harddrive: '160GB',Brand:'Hp'},
{ name: 'Hp probook 15.1',cpu:'2.4GHZ' ,ram: '4GB', harddrive: '160GB',Brand:'Hp'}
]
var laptop = new Laptop({laptopData});
laptop.save(function(err) {
// we've saved the dog into the db here
if (err) throw err;
});
答案 0 :(得分:1)
您应该单独创建对象并保存它们。你无法通过这样的数组。使用
行中的内容var laptopData = [...];
for(var laptopItem in laptopData){
new Laptop(laptopData[laptopItem])
.save()
.catch((err)=>{
console.log(err.message);
});
}