簡體   English   中英

玉不從JSON對象讀取數字元素

[英]Jade not reading number element from JSON object

我目前有一個數據庫(貓鼬),其架構定義為:

var CommentTableSchema = new mongoose.Schema({

    name: String,
    body: String,
    parentCommentID: String,
    depth: Number, // the depth of the comment to which it is nested.
});
CommentTableSchema.set('collection', 'comment');

我通過執行以下操作在數據庫中保存新評論:

var comment = mongoose.model('comment', CommentTableSchema);
        var currentComment = new comment({name: request.body.name, body: request.body.comment, parentCommentID: "" , depth: 1});
        currentComment.save(function (err, currentComment) {
            if (err) {
                console.log("saving error : " + currentComment);
            }
            else {
                console.log("saved!");
            }
        });

我將comment.find的結果傳遞到我的home.jade文件中。 在玉文件中,我有以下幾行(commentsTable是comment.find的結果)

-each comment in commentsTable
    div
        -console.log("comment depth: " + comment.depth)
        if comment.hasOwnProperty('depth') 
            |has depth
        else
            |no depth

這在我的瀏覽器頁面上輸出“無深度”。 但是,行-console.log("comment depth: " + comment.depth)在我的終端上提供了以下輸出:

comment depth: 1
comment depth: 1
comment depth: 1

這是預期的。 為什么翡翠無法讀取我的depth成員? 我在訪問home.jade文件中的comment.namecomment.bodycomment._id沒有問題。

在此回調中

currentComment.save(function (err, currentComment)

currentComment是一個貓鼬的Document對象

聽起來您想要的是簡單的結果,而不是貓鼬包裹的結果

我建議您在將對象傳遞到模型之前,調用此處引用的函數Document#toObject([options]) http://mongoosejs.com/docs/api.html#document_Document-toObject

同樣,執行查找操作時,您應該研究選項lean 當指定時,貓鼬返回未包裝的對象。

例如: http//mongoosejs.com/docs/api.html#model_Model.findById

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM