小编Noa*_*oah的帖子

给 Chai/Mocha 应该包含的部分键列表

看来,如果我这样做

describe( 'Add Youtube', function () {

    it( 'should return the video data, including user, title and content fields', function ( done ) {

        this.timeout( 5000 )

        request({
            method: 'POST',
            url: 'https://localhost:8443/api/add',
            json: true,
            strictSSL: false,
            body: {
                "type": "youtube",
                "url": "https://www.youtube.com/watch?v=uxfRLNiSikM"
            },
            headers: {
                "Authorization": "Bearer " + newTestUser.token
            } }, function ( err, response, body ) {

                body.should.include.keys( [ "user", "title", "content" ] )

                done()
        })
    })
})
Run Code Online (Sandbox Code Playgroud)

这将返回一个错误,因为返回的对象也有 key message。只要数组中的 3 个键存在,我怎么能让它回来,尽管还有更多。我不能总是预测每种情况下会发生什么。


更新:这是我如何要求 Chai 和should …

javascript unit-testing mocha.js node.js chai

3
推荐指数
1
解决办法
2757
查看次数

Mongoose post-remove事件不会触发

我在我的模型中有这个代码:

ContentSchema.post( 'remove', function( item ) {
    index.deleteObject( item._id )
})
Run Code Online (Sandbox Code Playgroud)

这是我的控制器中的内容:

Content.find( { user: user, _id: contentId } )
.remove( function ( err, count ) {
    if ( err || count == 0 ) reject( new Error( "There was an error deleting that content from the stream." ) )

    resolve( "Item removed from stream" )
})
Run Code Online (Sandbox Code Playgroud)

我希望当控制器中的函数运行时,模型中的函数应该发生.我可以在调试器中看到它根本不会触发.

我正在使用"mongoose": "3.8.23""mongoose-q": "0.0.16".

mongoose mongodb node.js mongoose-q

3
推荐指数
1
解决办法
2096
查看次数

Mongoose模式方法是"不是函数"

我的模型看起来像这样,但是当我尝试使用时verifyPassword,它说TypeError: user.verifyPassword is not a function

const User = new mongoose.Schema({
  name: {
    type: String,
    required: true
  },
  password: {
    type: String,
    required: true
  },
  avatar: String,
  token: String,
  role: String,
  permissions: Array,
  email: {
    type: String,
    unique: true,
    required: true
  },
  joined: {
    type: Number,
    default: ( new Date() * 1 )
  }
})

User.methods.verifyPassword = function (password) {
  return bcrypt.compare(password, this.password)
}
Run Code Online (Sandbox Code Playgroud)

我这样用它.

yield User.find({
  email: this.request.body.email
}).exec()
.then( user => {
  if ( user.verifyPassword(self.request.body.password) …
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb node.js koa

1
推荐指数
1
解决办法
3549
查看次数

方法名必须是字符串,PHP中的错误

这是我的代码,

class Tasks {
    public $parent;

    public function __construct($parent) {
        $this->parent = $parent;
    }

    public function get_task($parent) {
        return mysqli_query($db, "SELECT task, status, created_at FROM tasks WHERE parent IS '$parent' AND user='$user_id'");
    }
}

$project = new Tasks("NULL");

print "<div class='project'>" . $project->$get_task() . "</div>";
Run Code Online (Sandbox Code Playgroud)

我希望这应该将"NULL"传递给mysql_query并返回结果.但我得到错误,

Fatal error: Method name must be a string
Run Code Online (Sandbox Code Playgroud)

更新:为了解决这个问题,我只是没有创建类的实例.相反,我将我想要的值传递给函数Tasks::get_task(value-here).

php oop methods mysqli class

-1
推荐指数
1
解决办法
5686
查看次数