Save() 不会保存所有数据 (Mongoose)

问题描述 投票:0回答:1

所以这是我的问题:我猜这是一个异步问题,但

save()
方法仅保存所有文档的三个属性(带值):

fetch("***")
    .then(response => response.json())
    .then(data => {

    for (const fruit of data) {
        let name = fruit.name;
        let id = fruit.id;
        let family = fruit.family;
        let order = fruit.order;
        let genus = fruit.genus;
        let calories = fruit.nutritions.calories;
        let fat = fruit.nutritions.fat;
        let sugar = fruit.nutritions.sugar;
        let carbohydrates = fruit.nutritions.carbohydrates;
        let protein = fruit.nutritions.protein;

        let newFruit = new db({
            name: name,
            id: id,
            family: family,
            order: order,
            genus: genus,
            nutritions: { calories: calories, fat: fat, sugar: sugar, carbohydrates: carbohydrates, protein: protein }
        })
    
       newFruit.save()
    }
});

我尝试制作一个异步函数并使其与其中的

await
同步。像这样:

    .then(response => response.json())
    .then(data => {

    async function saveFruits (){

    for (const fruit of data) {
        let name = fruit.name;
        let id = fruit.id;
        let family = fruit.family;
        let order = fruit.order;
        let genus = fruit.genus;
        let calories = fruit.nutritions.calories;
        let fat = fruit.nutritions.fat;
        let sugar = fruit.nutritions.sugar;
        let carbohydrates = fruit.nutritions.carbohydrates;
        let protein = fruit.nutritions.protein;

        let newFruit = new db({
            name: name,
            id: id,
            family: family,
            order: order,
            genus: genus,
            nutritions: { calories: calories, fat: fat, sugar: sugar, carbohydrates: carbohydrates, protein: protein }
        })

        await newFruit.save()
    }}
});````


Doesn't work;

database loops mongoose save
1个回答
0
投票

console.log(newfruit.save())
给出:

{
  name: 'Persimmon',
  family: 'Ebenaceae',
  _id: new ObjectId('661fb49f67265c3cbeda5a4e')
}

...

...还有其余的水果..

空数据库模型是:

{
  "count": 0,
  "fields": []
}
save() 之后的 db 模型给出:

{ "count": 49, "fields": [ { "name": "_id", "path": [ "_id" ], "count": 49, "type": "ObjectId", "probability": 1, "hasDuplicates": false, "types": [ { "name": "ObjectId", "path": [ "_id" ], "count": 49, "probability": 1, "unique": 49, "hasDuplicates": false, "values": [ "661fd3705f3dc6104278752e", "661fd3705f3dc6104278754d", "661fd3705f3dc6104278754b", "661fd3705f3dc6104278753a", "661fd3705f3dc6104278754c", "661fd3705f3dc61042787520", "661fd3705f3dc6104278752b", "661fd3705f3dc61042787547", "661fd3705f3dc61042787533", "661fd3705f3dc61042787523", "661fd3705f3dc6104278753c", "661fd3705f3dc61042787526", "661fd3705f3dc61042787548", "661fd3705f3dc6104278753e", "661fd3705f3dc61042787521", "661fd3705f3dc6104278752d", "661fd3705f3dc61042787545", "661fd3705f3dc6104278753b", "661fd3705f3dc61042787528", "661fd3705f3dc61042787530", "661fd3705f3dc61042787537", "661fd3705f3dc6104278753d", "661fd3705f3dc61042787535", "661fd3705f3dc6104278751e", "661fd3705f3dc61042787539", "661fd3705f3dc6104278752f", "661fd3705f3dc61042787524", "661fd3705f3dc61042787538", "661fd3705f3dc6104278752c", "661fd3705f3dc6104278751f", "661fd3705f3dc61042787525", "661fd3705f3dc61042787549", "661fd3705f3dc61042787540", "661fd3705f3dc61042787544", "661fd3705f3dc61042787543", "661fd3705f3dc61042787522", "661fd3705f3dc61042787541", "661fd3705f3dc61042787532", "661fd3705f3dc61042787536", "661fd3705f3dc61042787529", "661fd3705f3dc61042787531", "661fd3705f3dc61042787542", "661fd3705f3dc6104278753f", "661fd3705f3dc6104278751d", "661fd3705f3dc61042787546", "661fd3705f3dc6104278752a", "661fd3705f3dc6104278754a", "661fd3705f3dc61042787527", "661fd3705f3dc61042787534" ], "bsonType": "ObjectId" } ] }, { "name": "__v", "path": [ "__v" ], "count": 49, "type": "Int32", "probability": 1, "hasDuplicates": true, "types": [ { "name": "Int32", "path": [ "__v" ], "count": 49, "probability": 1, "unique": 1, "hasDuplicates": true, "values": [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], "bsonType": "Int32" } ] }, { "name": "family", "path": [ "family" ], "count": 49, "type": "String", "probability": 1, "hasDuplicates": true, "types": [ { "name": "String", "path": [ "family" ], "count": 49, "probability": 1, "unique": 26, "hasDuplicates": true, "values": [ "Rosaceae", "Salicaceae", "Caricaceae", "Lythraceae", "Annonaceae", "Solanaceae", "Rosaceae", "Rutaceae", "Rosaceae", "Rosaceae", "Vitaceae", "Sapindaceae", "Clusiaceae", "Myrtaceae", "Rosaceae", "Rosaceae", "Cucurbitaceae", "Cactaceae", "Moraceae", "Rutaceae", "Rutaceae", "Moraceae", "Rosaceae", "Rosaceae", "Rutaceae", "Cucurbitaceae", "Ericaceae", "Cactaceae", "Rutaceae", "Musaceae", "Actinidiaceae", "Cucurbitaceae", "Actinidiaceae", "Moraceae", "Rosaceae", "Malvaceae", "Ericaceae", "Rosaceae", "Cucurbitaceae", "Grossulariaceae", "Anacardiaceae", "Rosaceae", "Lauraceae", "Ebenaceae", "Betulaceae", "Passifloraceae", " Ebenaceae", "Bromeliaceae", "Myrtaceae" ], "bsonType": "String" } ] }, { "name": "name", "path": [ "name" ], "count": 49, "type": "String", "probability": 1, "hasDuplicates": false, "types": [ { "name": "String", "path": [ "name" ], "count": 49, "probability": 1, "unique": 49, "hasDuplicates": false, "values": [ "Raspberry", "Ceylon Gooseberry", "Papaya", "Pomegranate", "Annona", "Tomato", "Plum", "Pomelo", "Apple", "Blackberry", "Grape", "Lychee", "Mangosteen", "Feijoa", "Pear", "GreenApple", "Horned Melon", "Dragonfruit", "Fig", "Lemon", "Tangerine", "Morus", "Apricot", "Strawberry", "Lime", "Watermelon", "Lingonberry", "Pitahaya", "Orange", "Banana", "Kiwi", "Pumpkin", "Kiwifruit", "Jackfruit", "Peach", "Durian", "Cranberry", "Blueberry", "Melon", "Gooseberry", "Mango", "Cherry", "Avocado", "Persimmon", "Hazelnut", "Passionfruit", "Japanese Persimmon", "Pineapple", "Guava" ], "bsonType": "String" } ] } ] }


    

© www.soinside.com 2019 - 2024. All rights reserved.