Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AssertionError:目标不能为null或未定义_Javascript_Node.js_Mocha.js - Fatal编程技术网

Javascript AssertionError:目标不能为null或未定义

Javascript AssertionError:目标不能为null或未定义,javascript,node.js,mocha.js,Javascript,Node.js,Mocha.js,我正在开发这个测试套件,这让我很沮丧,因为我继续遇到以下错误: 1) BlogPost API resource GET endpoint should return all existing posts: AssertionError: Target cannot be null or undefined. at D:\Projects\Thinkful\mongooseBlog02\blog-app-mongoose-challenge-

我正在开发这个测试套件,这让我很沮丧,因为我继续遇到以下错误:

1) BlogPost API resource
       GET endpoint
         should return all existing posts:
     AssertionError: Target cannot be null or undefined.
      at D:\Projects\Thinkful\mongooseBlog02\blog-app-mongoose-challenge-solution\test\test-blog-integration.js:128:54
      at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:188:7)
我尝试了
帖子
所有帖子
,但仍然出现同样的错误。我从我的
models.js
文件中获得了
allPosts

describe('GET endpoint', function() {

    it('should return all existing posts', function() {
      // strategy:
      //    1. get back all restaurants returned by by GET request to `/restaurants`
      //    2. prove res has right status, data type
      //    3. prove the number of restaurants we got back is equal to number
      //       in db.
      //
      // need to have access to mutate and access `res` across
      // `.then()` calls below, so declare it here so can modify in place
      let res;
      return chai.request(app)
        .get('/posts')
        .then(function(_res) {
          // so subsequent .then blocks can access response object
          res = _res;
          expect(res).to.have.status(200);
          console.log("testKC");
          // otherwise our db seeding didn't work
          expect(res.body.posts).to.have.lengthOf.at.least(1);
          return allPosts.count();
        })
        .then(function(count) {
          expect(res.body.posts).to.have.lengthOf(count);
        });
    });
'use strict';

const mongoose = require('mongoose');
mongoose.Promise = global.Promise;

const blogPostSchema = mongoose.Schema({
  author: {
    firstName: String,
    lastName: String
  },
  title: {type: String, required: true},
  content: {type: String},
  created: {type: Date, default: Date.now()}
});


blogPostSchema.virtual('authorName').get(function() {
  return `${this.author.firstName} ${this.author.lastName}`.trim();
});

blogPostSchema.methods.serialize = function() {
  return {
    id: this._id,
    author: this.author,
    content: this.content,
    title: this.title,
    created: this.created
  };
};

const BlogPost = mongoose.model('allPosts', blogPostSchema);

module.exports = { BlogPost };

它出现在您的一个expect语句中,您希望长度为XXX或类似的对象未定义或为空,并且没有要比较的length属性。因此,期望其长度为任意值的断言是失败的