验证MongoDB / Mogoose保存的数组

时间:2018-06-11 23:04:24

标签: mongodb mongoose assert

我对MongoDB / Mongoose保存操作后如何使用findOne方法感到茫然。

    // itemrefSchema.js
    const itemrefSchema = new mongoose.Schema({
        pubRef: [{
          type: mongoose.Schema.Types.Mixed,
          ref: 'pubRefs'
        }],
        itemSpec: [{
          type: mongoose.Schema.Types.Mixed,
          ref: 'pubRefs'
        }],
        munInspect: [{
          type: mongoose.Schema.Types.Mixed,
          ref: 'pubRefs'
        }]
      },
      { minimize: false }
    );
    const ItemRef = mongoose.model('itemRefs', itemrefSchema);
    module.exports = ItemRef;

// modelTest.js
it.only('find a record', (done) => {
        let reference = new ItemRef({
            pubRef: [{'publication': 'military pub 1'}, {'address': '../media/a001.pdf'}],
            itemSpec: [{'publication': 'item spec 1'}, {'address': '../media/a001spec.pdf'}],
            munInspect: [{'publication': 'mun inspection 1'}, {'address': '../media/a001inspec.pdf'}]
        });
        reference.save().then(() => {
            /*ItemRef.
                find().
                where('pubRef').equals('pulication').
                select('publication address').
                exec(() => {
                    console.log(callback);
                    done();
                });
            */
        //});

            ItemRef.findOne({'ItemRef': 'pubRef'})
                .then((result) => {
                    console.log(result);
                    //assert(result.publication === 'DD Form 626');
                    done();
                });

            //console.log(result);
            //done();
        });
    });

我知道如果我取消注释console.log(result)并注释掉findOne方法,则会保存参考数据。我从Mongoose的承诺中得到了数据:

{ _id: 5b1ef810e8609509bc81fa19,
  createdAt: 2018-06-11T22:30:40.216Z,
  updatedAt: 2018-06-11T22:30:40.216Z,
  __v: 0,
  munInspect:
   [ { publication: 'mun inspection 1' },
     { address: '../media/a001inspec.pdf' } ],
  itemSpec:
   [ { publication: 'item spec 1' },
     { address: '../media/a001spec.pdf' } ],
  pubRef:
   [ { publication: 'military pub 1' },
     { address: '../media/a001.pdf' } ] }
    √ find a record (150ms)

我试图通过选择任何munInspect并验证MongoDB保存内容来构造该断言。当我执行findOne方法时,我收到了#null;'的响应。任何的意见都将会有帮助。我哪里出错了?

1 个答案:

答案 0 :(得分:0)

您可以使用保存功能

尝试以下操作
if(empty($errorMessage)) 
{
    $fs = fopen("data.html","w");
    fwrite($fs, "<table > <tr><th>No</th> <th>Name</th> <th>Contact</th> <th>Books</th></tr>");
    fwrite($fs,"
    <tr>
         <td> ".$varaa. "</td>
        <td>" .$varab. "</td>
        <td>" .$varac. "</td>
    </tr>");
    fwrite($fs, "</table >");
    fclose($fs);

    header("Location: here.html");
    exit;
}