2

I'm using mongo with NODEJS, but while trying to get data from db, there is always an object it returns [object Object] instead of object; Here is my Schema

IngredientSchema = new MongoDb.Schema(
    {
        "name": String  //名称
        ,"optional":Boolean
        ,"substitution": String
        ,"preparation": String
        , "amount": Number
        , "units": String   //单位
    }
);

directionSchema = new MongoDb.Schema(
    {
        "image": String     //图片
        ,"tip": String
        , "content": String //正文
        , "duration": Number    //定时 无定时则设置为null
        , "audiopath": String
    }
);

infoSchema =  {
    "title": String //菜谱名称
    ,"lang": String
    ,"author":String
    ,"image_url": String
    ,"serves": Number
    ,"source": String
    ,"time":Number
};

categorySchema = {
    "region": String
    ,"type": String
    ,"tags": String
};

CookBookSchema= new MongoDb.Schema(
    {
        "info": infoSchema
        , "categories": categorySchema
        , "ingredients": [IngredientSchema] //材料
        , "directions": [directionSchema]       //步骤
        ,'nutrition':String
    }
    , {strict: false}
);
CookBook= MongoDb.model(COOKBOOK_TABLE, CookBookSchema);

it returns value like this

[
    {
        "categories": "[object Object]",
        ....
    }
]

The other object in result works fine. I searched in google but can't find the answer

In mongodb the "categories" data is like this

categories" : { "type" : "cake", "tags" : "Cake, Caramel Sauce, Chocolate Cake, Chocolate Cake, Chocolate Desserts, Christmas, Christmas Cake, Christmas Desserts, Christmas Party, Coconuts, Desserts, Fruit, German Chocolate Cake, Healthy Cooking, Ice Cream Toppings, Low Sodium, More Fruits, More Holidays, Sauces and Toppings, Valentine's Day" }

But it lack of an _id object which other objects will have

i wrote into mongodb using codes below

```

cookBookModule.create(data, function(err, object) {
            if (err) {
                console.error(err);
                console.log(data);
                return res.status(HTTPCODE.DB_ERR).json('save failed');
            }

            res.status(HTTPCODE.CREATED).json(object);
            TtsModule.audio_process(object);
        })

```

Is there anything wrong?

6
  • 2
    use JSON.stringify() method to see what your [object object] contains. Commented Sep 22, 2014 at 9:24
  • yes i did. I printed it with JSON.stringify(). This is not the matter. It just return a string, not an object Commented Sep 22, 2014 at 9:26
  • Use "util" Package to view the object.. Commented Sep 22, 2014 at 9:26
  • How to? never used utile Commented Sep 22, 2014 at 9:27
  • I don't use Mongoose but shouldn't it be categorySchema = new MongoDb.Schema({...})? Commented Sep 22, 2014 at 9:33

1 Answer 1

1
"categories": "[object Object]"

This means that when you stored the object, you actually stored the toString() representation of it. As there was none, you got the default [object Object]

Your issue is not on the reading from the database, but on the writing, check how your categories are being serialized into mongo.

Sign up to request clarification or add additional context in comments.

2 Comments

you are right,But when I looked into Mongodb, the "category" is an object, But it's strange that usually objects in Mongo will have an _id object, but this one has none. and also i use Module.create(data, function(err, object){...});Is there anything needs attention?
what is inside the data? that's what you should be looking at. Categories don't output an _id because they are inside your cookbook, so the cookbook will have the ID.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.