我已经写了使用jsonfile.writeFile.我想写一个异步JSON接触,这将覆盖
这里,文件contact.js
我data.json文件的内容是我的contact.js代码:
var Contact = {}
Contact.saveContacts=function(contacts,done){
var jsonfile = require('jsonfile')
jsonfile.writeFile('data.json',contacts, done, function(err){
done(err)
})
}
module.exports=Contact
这里的测试.js文件
var contacts = [ { name: "John Smith", number: "604-123-9090" } ]
Contact.saveContacts(contacts, function(err) {
console.log('success')
})
我得到以下错误:
Error: Contact.saveContacts did not write any data or the written data is incorrect.
我该如何解决这个问题?就像你写
Contact.saveContacts = function(contacts, done) {
var jsonfile = require('jsonfile');
jsonfile.writeFile('data.json', contacts, done);
}
而且你test.js:
我已经尝试过了,在所有:( – orvi
我复制你的源代码,以尽量模拟你的情况没有什么好运气,但我不能。我在Ubuntu和Windows做同样的事情,两种情况我可以编写这个文件,你是否尝试用另一个文件创建另一个目录并运行? – Luidy
好的我解决了这个问题 – orvi