admin管理员组文章数量:1302407
I am working on this testing suite that is frustrating me because I continue to get this error:
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)
This is in reference to line 128 in my test-blog-integration.js
file:
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);
});
});
I have tried posts
, allPosts
, but I continue to get the same error. I got allPosts
from my models.js
file:
'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 };
I am working on this testing suite that is frustrating me because I continue to get this error:
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)
This is in reference to line 128 in my test-blog-integration.js
file:
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);
});
});
I have tried posts
, allPosts
, but I continue to get the same error. I got allPosts
from my models.js
file:
'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 };
Share
Improve this question
asked Jul 17, 2018 at 1:45
user8359832user8359832
1 Answer
Reset to default 8It appears in one of your expect statements, the object you are expecting to have a length of XXX or such is undefined or null and does not have length attribute to pare to. Thus the assertion of expecting it for have a length of any value is failing.
本文标签: javascriptAssertionError Target cannot be null or undefinedStack Overflow
版权声明:本文标题:javascript - AssertionError: Target cannot be null or undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741708267a2393687.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论